<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh" lang="zh">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<meta name="Keywords" content="Zope, Plone, bfg, 开源社区">
<meta content="这里是Zope开源web应用服务器和Plone开源内容管理系统的中文技术社区。" name="description">
<!-- Plone ECMAScripts --><style type="text/css">
            .tabber{display:none}
        </style>
<style type="text/css">
            <!-- @import url(http://everydo.com/themes/czug.org/css/ploneStyles6234.css); -->
        </style>
<link rel="alternate stylesheet" type="text/css" media="screen" href="http://everydo.com/themes/czug.org/css/ploneStyles0053.css" title="Small Text">
<link rel="alternate stylesheet" type="text/css" media="screen" href="http://everydo.com/themes/czug.org/css/ploneStyles2144.css" title="Large Text">
<style type="text/css" media="all">
            <!-- @import url(http://everydo.com/themes/czug.org/css/ploneStyles8099.css); -->
        </style>
<link rel="stylesheet" href="http://everydo.com/themes/czug.org/css/navtree.css" type="text/css">
<script type="text/javascript" src="http://everydo.com/resources/thecmsface/js/sarissa.js">
        </script><script type="text/javascript" src="http://everydo.com/resources/thecmsface/js/base2-dom-fp.js">
        </script><script type="text/javascript" src="http://everydo.com/resources/thecmsface/js/kukit.js">
        </script><script type="text/javascript" src="http://everydo.com/resources/thecmsface/js/zopen.js">
        </script><link type="text/css" rel="kinetic-stylesheet" href="http://czug.org/static/navtree.kss" media="all">
<!-- Internet Explorer CSS Fixes --><!--[if IE]>
            <style type="text/css" media="all">
                @import url(IEFixes.css);
            </style>
        <![endif]--><link rel="shortcut icon" type="image/x-icon" href="http://everydo.com/themes/czug.org/img/favicon.ico">
<link rel="home" href="http://czug.org" title="Front page">
<link rel="search" href="http://everydo.com/themes/czug.org/search_form" title="Search this site">
<link rel="author" href="http://everydo.com/themes/czug.org/author/panjy" title="Author information">
<link rel="contents" href="http://everydo.com/sitemap" title="Site Map">
<!-- Disable IE6 image toolbar --><meta http-equiv="imagetoolbar" content="no">
<title>第十三章 建造列表控件并管理列表项 - 中国Zope/Plone用户组</title>
</head>
<body class="section-index_html" dir="ltr">
        <div id="visual-portal-wrapper">
            <div id="portal-top">
                <div id="portal-header">
                    <p class="hiddenStructure">
                        <a accesskey="2" href="http://everydo.com/themes/czug.org/index.html?notheme#documentContent">
                            Skip to content.
                        </a>
                        |
                        <a accesskey="6" href="http://everydo.com/themes/czug.org/index.html?notheme#portlet-navigation-tree">
                            Skip to navigation
                        </a>
                    </p>
                    <h1 id="portal-logo">
                        <a href="http://czug.org" accesskey="1">
                            中国Zope/Plone用户组
                        </a>
                    </h1>
                    <div id="portal-skinswitcher">
                    </div>
                    <h5 class="hiddenStructure">
                        Sections
                    </h5>
                    <ul id="portal-globalnav">
<li id="首页" class="plain">
                        <a href="/">首页</a>
                      </li>
<li id="Zope" class="plain">
                        <a href="/zope">Zope</a>
                      </li>
<li id="Plone" class="plain">
                        <a href="/plone">Plone</a>
                      </li>
<li id="Python" class="selected">
                        <a href="/python">Python</a>
                      </li>
<li id="社区项目" class="plain">
                        <a href="/projects">社区项目</a>
                      </li>
<li id="博客群" class="plain">
                        <a href="/blog/">博客群</a>
                      </li>
<li id="关于" class="plain">
                        <a href="/about">关于</a>
                      </li>
</ul>
</div>
                <div id="portal-personaltools-wrapper">
                    <h5 class="hiddenStructure">
                        Personal tools
                    </h5>
                </div>
                <!-- <div id="portal-breadcrumbs">
                <span id="breadcrumbs-you-are-here">You
                are here:</span>
                <a href="http://czug.org">Home</a>
                </div>
                -->
            </div>
            <div class="visualClear" id="clear-space-before-wrapper-table">
                <!-- -->
            </div>
            <table id="portal-columns"><tbody><tr id="portal-tr">
<td id="portal-column-one">
            <div class="visualPadding"> <div class="document">

<div id="leftcolumn" class="edoBox">
  <b class="xtop edoxtop">
    <b class="xb1"></b>
    <b class="xb2"></b>
    <b class="xb3"></b>
    <b class="xb4"></b>
 </b>
 <b class="itemxtop edoitemxtop">
    <b class="itemxb1"></b>
    <b class="itemxb2"></b>
    <b class="itemxb3"></b>
    <b class="itemxb4"></b>
 </b>
 <dl class="portlet portlet-navigation-tree">
<dt class="portletHeader">
        <a class="title" href="http://www.czug.org/python/wxpythoninaction/">《活学活用wxPython》</a>
    </dt>
    <dd class="portletItem">
        <ul class="portletNavigationTree navTreeLevel0">
<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/pr01.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="pr01.htm" href="http://www.czug.org/python/wxpythoninaction/pr01.htm" onfocus="this.blur()">
        
        
        前言
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/pr02.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="pr02.htm" href="http://www.czug.org/python/wxpythoninaction/pr02.htm" onfocus="this.blur()">
        
        
        关于本书
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch01.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="ch01.htm" href="http://www.czug.org/python/wxpythoninaction/ch01.htm" onfocus="this.blur()">
        
        
        第一章 欢迎使用wxPython
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch02.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="ch02.htm" href="http://www.czug.org/python/wxpythoninaction/ch02.htm" onfocus="this.blur()">
        
        
        第二章 给wxPython程序一个坚实的基础
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch03.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="ch03.htm" href="http://www.czug.org/python/wxpythoninaction/ch03.htm" onfocus="this.blur()">
        
        
        第三章 在事件驱动环境中开发
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch04.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="ch04.htm" href="http://www.czug.org/python/wxpythoninaction/ch04.htm" onfocus="this.blur()">
        
        
        第四章 用PyCrust使得wxPython更易处理
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch05.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="ch05.htm" href="http://www.czug.org/python/wxpythoninaction/ch05.htm" onfocus="this.blur()">
        
        
        第五章 绘制蓝图
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch06.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="ch06.htm" href="http://www.czug.org/python/wxpythoninaction/ch06.htm" onfocus="this.blur()">
        
        
        第六章 使用wxPython基本构件
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch07.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="ch07.htm" href="http://www.czug.org/python/wxpythoninaction/ch07.htm" onfocus="this.blur()">
        
        
        第七章 使用基础控件
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch08.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="ch08.htm" href="http://www.czug.org/python/wxpythoninaction/ch08.htm" onfocus="this.blur()">
        
        
        第八章 将构件放入窗体中
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch09.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="ch09.htm" href="http://www.czug.org/python/wxpythoninaction/ch09.htm" onfocus="this.blur()">
        
        
        第九章 通过对话框让用户选择
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch10.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="ch10.htm" href="http://www.czug.org/python/wxpythoninaction/ch10.htm" onfocus="this.blur()">
        
        
        第十章 创建和使用wxPython菜单
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch11.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="ch11.htm" href="http://www.czug.org/python/wxpythoninaction/ch11.htm" onfocus="this.blur()">
        
        
        第十一章 使用sizer放置构件
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch12.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="ch12.htm" href="http://www.czug.org/python/wxpythoninaction/ch12.htm" onfocus="this.blur()">
        
        
        第十二章 操作基本图像
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node selected notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch13.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        navTreeCurrentItem" name="ch13.htm" href="http://www.czug.org/python/wxpythoninaction/ch13.htm" onfocus="this.blur()">
        
        
        第十三章 建造列表控件并管理列表项
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch14.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="ch14.htm" href="http://www.czug.org/python/wxpythoninaction/ch14.htm" onfocus="this.blur()">
        
        
        第十四章 网格控件
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch15.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="ch15.htm" href="http://www.czug.org/python/wxpythoninaction/ch15.htm" onfocus="this.blur()">
        
        
        第十五章 树形控件
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch16.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="ch16.htm" href="http://www.czug.org/python/wxpythoninaction/ch16.htm" onfocus="this.blur()">
        
        
        第十六章 在应用程序中加入HTML
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch17.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="ch17.htm" href="http://www.czug.org/python/wxpythoninaction/ch17.htm" onfocus="this.blur()">
        
        
        第十七章 wxPython的打印构架
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch18.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="ch18.htm" href="http://www.czug.org/python/wxpythoninaction/ch18.htm" onfocus="this.blur()">
        
        
        第十八章 使用wxPython的其他功能
            </a>
    </div>
    
</li>

            <!--p class="navTreeItem visualNoMarker" 
               tal:repeat="node items" 
               tal:replace="structure node">
            </p-->
        </ul>
</dd>
    <dd class="portletFooter">
        <div><!-- --></div>
    </dd>
  </dl>
<b class="xbottom"><b class="xb4"></b><b class="xb3"></b><b class="xb2"></b><b class="xb1"></b></b>
</div>
</div>
 </div> </td>
<td id="portal-column-content">
                            <div id="content" class="documentContent">
<h1>第十三章 建造列表控件并管理列表项</h1>
<div lang="en" id="content" dir="ltr">
<span id="top" class="anchor"></span>
<span id="line-1" class="anchor"></span><p class="line867"><strong>13</strong>  <span id="line-2" class="anchor"></span><strong>建造列表控件并管理项目</strong> <span id="line-3" class="anchor"></span><span id="line-4" class="anchor"></span></p>
<p class="line874">本章内容： <span id="line-5" class="anchor"></span><span id="line-6" class="anchor"></span></p>
<ul>
<li>创建不同样式的列表控件 <span id="line-7" class="anchor"></span><span id="line-8" class="anchor"></span>
</li>
<li class="gap">处理列表中的项目 <span id="line-9" class="anchor"></span><span id="line-10" class="anchor"></span>
</li>
<li class="gap">响应列表中用户的选择 <span id="line-11" class="anchor"></span><span id="line-12" class="anchor"></span>
</li>
<li class="gap">编辑标签和对列表排序 <span id="line-13" class="anchor"></span><span id="line-14" class="anchor"></span>
</li>
<li class="gap">创建大的列表控件 <span id="line-15" class="anchor"></span><span id="line-16" class="anchor"></span><span id="line-17" class="anchor"></span>
</li>
</ul>
<p class="line862">在<tt>wxPython</tt>中，有两个控件，你可以用来显示基于列表的信息。较简单的是列表框，它是可滚动的单列列表，类似于使用<tt>HTML</tt>的 <tt>select </tt>标记所得到的。列表框已经在第8章中讨论过了，本章不再作进一步的讨论。 <span id="line-18" class="anchor"></span><span id="line-19" class="anchor"></span></p>
<p class="line874">本章讨论较为复杂的列表：列表控件，它是一个完整特性的列表窗口部件。这个列表控件可以每行显示多列信息，并可基于任一行进行排序，还能以不同的样式显示。对于列表控件的每个部分的细节显示，你有很大的灵活性。 <span id="line-20" class="anchor"></span><span id="line-21" class="anchor"></span><span id="line-22" class="anchor"></span></p>
<p class="line867">
</p>
<h2 id="head-6add6a375d8f2b8191d6d221b805a98d9a4e5eea">1. 建造一个列表控件</h2>
<span id="line-23" class="anchor"></span><span id="line-24" class="anchor"></span><span id="line-25" class="anchor"></span><p class="line874">列表控件能够以下面四种不同模式建造： <span id="line-26" class="anchor"></span><span id="line-27" class="anchor"></span></p>
<ul>
<li><p class="line862">图标(<tt>icon)</tt> <span id="line-28" class="anchor"></span><span id="line-29" class="anchor"></span></p></li>
<li class="gap"><p class="line862">小图标(<tt>small icon)</tt> <span id="line-30" class="anchor"></span><span id="line-31" class="anchor"></span></p></li>
<li class="gap"><p class="line862">列表(<tt>list)</tt> <span id="line-32" class="anchor"></span><span id="line-33" class="anchor"></span></p></li>
<li class="gap"><p class="line862">报告(<tt>report)</tt> <span id="line-34" class="anchor"></span><span id="line-35" class="anchor"></span><span id="line-36" class="anchor"></span></p></li>
</ul>
<p class="line862">如果你用过微软<tt>Windows</tt>的资源管理器(<tt>Explorer)</tt>或<tt>Mac</tt>的<tt>Finder</tt>，那么这些方式你应该熟悉。我们将通过说明如何建立四种不同模式的列表作为开始来介绍列表控件。 <span id="line-37" class="anchor"></span><span id="line-38" class="anchor"></span><span id="line-39" class="anchor"></span></p>
<p class="line867">
</p>
<h3 id="head-af3070ed506470f1af8d2023fca388bc97c8974c">1.1. 什么是图标模式？</h3>
<span id="line-40" class="anchor"></span><span id="line-41" class="anchor"></span><span id="line-42" class="anchor"></span><p class="line862">列表控件看起来类似于微软的<tt>Windows</tt>资源管理器的一个文件树系统的显示面板。它以四种模式之一的一种显示一个信息的列表。默认模式是图标模式，显示在列表中的每个元素都是一个其下带有文本的一个图标。图13.1显示了一个图标模式的列表。 <span id="line-43" class="anchor"></span><span id="line-44" class="anchor"></span></p>
<p class="line862">例13.1是产生图13.1的代码。注意这个例子使用了同目录下的一些.<tt>png</tt>文件。 <span id="line-45" class="anchor"></span><span id="line-46" class="anchor"></span><span id="line-47" class="anchor"></span></p>
<p class="line867"><strong>例13.1</strong>  <span id="line-48" class="anchor"></span><strong>创建一个图标模式的列表</strong> <span id="line-49" class="anchor"></span><span id="line-50" class="anchor"></span><span id="line-51" class="anchor"></span><span id="line-52" class="anchor"></span><span id="line-53" class="anchor"></span><span id="line-54" class="anchor"></span><span id="line-55" class="anchor"></span><span id="line-56" class="anchor"></span><span id="line-57" class="anchor"></span><span id="line-58" class="anchor"></span><span id="line-59" class="anchor"></span><span id="line-60" class="anchor"></span><span id="line-61" class="anchor"></span><span id="line-62" class="anchor"></span><span id="line-63" class="anchor"></span><span id="line-64" class="anchor"></span><span id="line-65" class="anchor"></span><span id="line-66" class="anchor"></span><span id="line-67" class="anchor"></span><span id="line-68" class="anchor"></span><span id="line-69" class="anchor"></span><span id="line-70" class="anchor"></span><span id="line-71" class="anchor"></span><span id="line-72" class="anchor"></span><span id="line-73" class="anchor"></span><span id="line-74" class="anchor"></span><span id="line-75" class="anchor"></span><span id="line-76" class="anchor"></span><span id="line-77" class="anchor"></span><span id="line-78" class="anchor"></span><span id="line-79" class="anchor"></span><span id="line-80" class="anchor"></span><span id="line-81" class="anchor"></span><span id="line-82" class="anchor"></span><span id="line-83" class="anchor"></span><span id="line-84" class="anchor"></span><span id="line-85" class="anchor"></span></p>
<div lang="en" dir="ltr" class="codearea">
<script type="text/javascript">
function isnumbered(obj) {
return obj.childNodes.length && obj.firstChild.childNodes.length && obj.firstChild.firstChild.className == 'LineNumber';
}
function nformat(num,chrs,add) {
var nlen = Math.max(0,chrs-(''+num).length), res = '';
while (nlen>0) { res += ' '; nlen-- }
return res+num+add;
}
function addnumber(did, nstart, nstep) {
var c = document.getElementById(did), l = c.firstChild, n = 1;
if (!isnumbered(c))
if (typeof nstart == 'undefined') nstart = 1;
if (typeof nstep  == 'undefined') nstep = 1;
n = nstart;
while (l != null) {
if (l.tagName == 'SPAN') {
var s = document.createElement('SPAN');
s.className = 'LineNumber'
s.appendChild(document.createTextNode(nformat(n,4,' ')));
n += nstep;
if (l.childNodes.length)
l.insertBefore(s, l.firstChild)
else
l.appendChild(s)
}
l = l.nextSibling;
}
return false;
}
function remnumber(did) {
var c = document.getElementById(did), l = c.firstChild;
if (isnumbered(c))
while (l != null) {
if (l.tagName == 'SPAN' && l.firstChild.className == 'LineNumber') l.removeChild(l.firstChild);
l = l.nextSibling;
}
return false;
}
function togglenumber(did, nstart, nstep) {
var c = document.getElementById(did);
if (isnumbered(c)) {
remnumber(did);
} else {
addnumber(did,nstart,nstep);
}
return false;
}
</script><script type="text/javascript">
document.write('<a href="#" onclick="return togglenumber(\'CA-7c69c54a655a9ba9e0f8822da5b3b35c0e270d1c_000\', 1, 1);" \
class="codenumbers">切换行号显示<\/a>');
</script><a class="codenumbers" onclick="return togglenumber('CA-7c69c54a655a9ba9e0f8822da5b3b35c0e270d1c_000', 1, 1);" href="#">切换行号显示</a>
<pre lang="en" id="CA-7c69c54a655a9ba9e0f8822da5b3b35c0e270d1c_000" dir="ltr"><span class="line"><span class="LineNumber">   1 </span><span class="Comment">#-*- encoding:UTF-8 -*-</span></span>
<span class="line"><span class="LineNumber">   2 </span><span class="Comment"></span><span class="ResWord">import</span> <span class="ID">wx</span></span>
<span class="line"><span class="LineNumber">   3 </span><span class="ResWord">import</span> <span class="ID">sys</span><span class="Operator">,</span> <span class="ID">glob</span></span>
<span class="line"><span class="LineNumber">   4 </span></span>
<span class="line"><span class="LineNumber">   5 </span><span class="ResWord">class</span> <span class="ID">DemoFrame</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">Frame</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">   6 </span>    <span class="ResWord">def</span> <span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">   7 </span>        <span class="ID">wx</span><span class="Operator">.</span><span class="ID">Frame</span><span class="Operator">.</span><span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">None</span><span class="Operator">,</span> <span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">   8 </span>                          <span class="String">"wx.ListCtrl in wx.LC_ICON mode"</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">   9 </span>                          <span class="ID">size</span><span class="Operator">=</span><span class="Operator">(</span><span class="Number">600</span><span class="Operator">,</span><span class="Number">400</span><span class="Operator">)</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  10 </span></span>
<span class="line"><span class="LineNumber">  11 </span>        <span class="Comment"># load some images into an image list</span></span>
<span class="line"><span class="LineNumber">  12 </span><span class="Comment"></span>        <span class="ID">il</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">ImageList</span><span class="Operator">(</span><span class="Number">32</span><span class="Operator">,</span><span class="Number">32</span><span class="Operator">,</span> <span class="ID">True</span><span class="Operator">)</span><span class="Comment">#创建图像列表</span></span>
<span class="line"><span class="LineNumber">  13 </span>        <span class="ResWord">for</span> <span class="ID">name</span> <span class="ResWord">in</span> <span class="ID">glob</span><span class="Operator">.</span><span class="ID">glob</span><span class="Operator">(</span><span class="String">"icon??.png"</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  14 </span>            <span class="ID">bmp</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">Bitmap</span><span class="Operator">(</span><span class="ID">name</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">BITMAP_TYPE_PNG</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  15 </span>            <span class="ID">il_max</span> <span class="Operator">=</span> <span class="ID">il</span><span class="Operator">.</span><span class="ID">Add</span><span class="Operator">(</span><span class="ID">bmp</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  16 </span></span>
<span class="line"><span class="LineNumber">  17 </span>        <span class="Comment"># create the list control</span></span>
<span class="line"><span class="LineNumber">  18 </span><span class="Comment"></span><span class="Comment">#创建列表窗口部件</span></span>
<span class="line"><span class="LineNumber">  19 </span><span class="Comment"></span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">ListCtrl</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">  20 </span>                <span class="ID">style</span><span class="Operator">=</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">LC_ICON</span> <span class="Operator">|</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">LC_AUTOARRANGE</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  21 </span></span>
<span class="line"><span class="LineNumber">  22 </span>        <span class="Comment"># assign the image list to it</span></span>
<span class="line"><span class="LineNumber">  23 </span><span class="Comment"></span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">AssignImageList</span><span class="Operator">(</span><span class="ID">il</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">IMAGE_LIST_NORMAL</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  24 </span></span>
<span class="line"><span class="LineNumber">  25 </span>        <span class="Comment"># create some items for the list</span></span>
<span class="line"><span class="LineNumber">  26 </span><span class="Comment"></span><span class="Comment">#为列表创建一些项目</span></span>
<span class="line"><span class="LineNumber">  27 </span><span class="Comment"></span>        <span class="ResWord">for</span> <span class="ID">x</span> <span class="ResWord">in</span> <span class="ID">range</span><span class="Operator">(</span><span class="Number">25</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  28 </span>            <span class="ID">img</span> <span class="Operator">=</span> <span class="ID">x</span> <span class="Operator">%</span> <span class="Operator">(</span><span class="ID">il_max</span><span class="Operator">+</span><span class="Number">1</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  29 </span>            <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">InsertImageStringItem</span><span class="Operator">(</span><span class="ID">x</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">  30 </span>                    <span class="String">"This is item %02d"</span> <span class="Operator">%</span> <span class="ID">x</span><span class="Operator">,</span> <span class="ID">img</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  31 </span></span>
<span class="line"><span class="LineNumber">  32 </span><span class="ID">app</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">PySimpleApp</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  33 </span><span class="ID">frame</span> <span class="Operator">=</span> <span class="ID">DemoFrame</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  34 </span><span class="ID">frame</span><span class="Operator">.</span><span class="ID">Show</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  35 </span><span class="ID">app</span><span class="Operator">.</span><span class="ID">MainLoop</span><span class="Operator">(</span><span class="Operator">)</span><span class="Text"></span></span>
</pre>
</div>
<span id="line-86" class="anchor"></span><span id="line-87" class="anchor"></span><p class="line867"><strong>图13.1</strong> <span id="line-88" class="anchor"></span><span id="line-89" class="anchor"></span></p>
<p class="line867"><img title="attachment:w13.1.gif" src="/moin/WxPythonInAction/ChapterThirteen?action=AttachFile&amp;do=get&amp;target=w13.1.gif" class="attachment"><span id="line-90" class="anchor"></span><span id="line-91" class="anchor"></span><span id="line-92" class="anchor"></span></p>
<p class="line862">在例13.1中，<tt>DemoFrame</tt>创建了一个“<tt>image
list</tt>（图像列表）”来包含对要显示的图像的引用，然后它建造并扩充了这个列表控件。我们将在本章稍后的部分讨论“<tt>image list</tt>（图像列表）”。 <span id="line-93" class="anchor"></span><span id="line-94" class="anchor"></span><span id="line-95" class="anchor"></span></p>
<p class="line867">
</p>
<h3 id="head-0a2f5d63d3c5f50fef6af82511fbdc02b4aba4f2">1.2. 什么是小图标模式？</h3>
<span id="line-96" class="anchor"></span><span id="line-97" class="anchor"></span><span id="line-98" class="anchor"></span><p class="line874">小图标模式类似标准的图标模式，但是图标更小点。图13.2以小图标模式显示了相同的列表。 <span id="line-99" class="anchor"></span><span id="line-100" class="anchor"></span></p>
<p class="line874">当你想在窗口部件中放入更多的显示项目时，小图标模式是最有用的，尤其是当图标不够精细时。 <span id="line-101" class="anchor"></span><span id="line-102" class="anchor"></span><span id="line-103" class="anchor"></span></p>
<p class="line867"><strong>图13.2</strong> <span id="line-104" class="anchor"></span><span id="line-105" class="anchor"></span></p>
<p class="line867"><img title="attachment:w13.2.gif" src="/moin/WxPythonInAction/ChapterThirteen?action=AttachFile&amp;do=get&amp;target=w13.2.gif" class="attachment"><span id="line-106" class="anchor"></span><span id="line-107" class="anchor"></span><span id="line-108" class="anchor"></span></p>
<p class="line874">产生图13.2的示例代码如下： <span id="line-109" class="anchor"></span><span id="line-110" class="anchor"></span></p>
<p class="line867"><span id="line-111" class="anchor"></span><span id="line-112" class="anchor"></span><span id="line-113" class="anchor"></span><span id="line-114" class="anchor"></span><span id="line-115" class="anchor"></span><span id="line-116" class="anchor"></span><span id="line-117" class="anchor"></span><span id="line-118" class="anchor"></span><span id="line-119" class="anchor"></span><span id="line-120" class="anchor"></span><span id="line-121" class="anchor"></span><span id="line-122" class="anchor"></span><span id="line-123" class="anchor"></span><span id="line-124" class="anchor"></span><span id="line-125" class="anchor"></span><span id="line-126" class="anchor"></span><span id="line-127" class="anchor"></span><span id="line-128" class="anchor"></span><span id="line-129" class="anchor"></span><span id="line-130" class="anchor"></span><span id="line-131" class="anchor"></span><span id="line-132" class="anchor"></span><span id="line-133" class="anchor"></span><span id="line-134" class="anchor"></span><span id="line-135" class="anchor"></span><span id="line-136" class="anchor"></span><span id="line-137" class="anchor"></span><span id="line-138" class="anchor"></span><span id="line-139" class="anchor"></span><span id="line-140" class="anchor"></span><span id="line-141" class="anchor"></span><span id="line-142" class="anchor"></span><span id="line-143" class="anchor"></span><span id="line-144" class="anchor"></span><span id="line-145" class="anchor"></span><span id="line-146" class="anchor"></span><span id="line-147" class="anchor"></span></p>
<div lang="en" dir="ltr" class="codearea">
<script type="text/javascript">
document.write('<a href="#" onclick="return togglenumber(\'CA-2b77e4fd853f5a745451b272ffb7ed6c93b9e635_001\', 1, 1);" \
class="codenumbers">切换行号显示<\/a>');
</script><a class="codenumbers" onclick="return togglenumber('CA-2b77e4fd853f5a745451b272ffb7ed6c93b9e635_001', 1, 1);" href="#">切换行号显示</a>
<pre lang="en" id="CA-2b77e4fd853f5a745451b272ffb7ed6c93b9e635_001" dir="ltr"><span class="line"><span class="LineNumber">   1 </span><span class="ResWord">import</span> <span class="ID">wx</span></span>
<span class="line"><span class="LineNumber">   2 </span><span class="ResWord">import</span> <span class="ID">sys</span><span class="Operator">,</span> <span class="ID">glob</span></span>
<span class="line"><span class="LineNumber">   3 </span></span>
<span class="line"><span class="LineNumber">   4 </span><span class="ResWord">class</span> <span class="ID">DemoFrame</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">Frame</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">   5 </span>    <span class="ResWord">def</span> <span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">   6 </span>        <span class="ID">wx</span><span class="Operator">.</span><span class="ID">Frame</span><span class="Operator">.</span><span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">None</span><span class="Operator">,</span> <span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">   7 </span>                          <span class="String">"wx.ListCtrl in wx.LC_SMALL_ICON mode"</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">   8 </span>                          <span class="ID">size</span><span class="Operator">=</span><span class="Operator">(</span><span class="Number">600</span><span class="Operator">,</span><span class="Number">400</span><span class="Operator">)</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   9 </span></span>
<span class="line"><span class="LineNumber">  10 </span>        <span class="Comment"># load some images into an image list</span></span>
<span class="line"><span class="LineNumber">  11 </span><span class="Comment"></span>        <span class="ID">il</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">ImageList</span><span class="Operator">(</span><span class="Number">16</span><span class="Operator">,</span><span class="Number">16</span><span class="Operator">,</span> <span class="ID">True</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  12 </span>        <span class="ResWord">for</span> <span class="ID">name</span> <span class="ResWord">in</span> <span class="ID">glob</span><span class="Operator">.</span><span class="ID">glob</span><span class="Operator">(</span><span class="String">"smicon??.png"</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  13 </span>            <span class="ID">bmp</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">Bitmap</span><span class="Operator">(</span><span class="ID">name</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">BITMAP_TYPE_PNG</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  14 </span>            <span class="ID">il_max</span> <span class="Operator">=</span> <span class="ID">il</span><span class="Operator">.</span><span class="ID">Add</span><span class="Operator">(</span><span class="ID">bmp</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  15 </span></span>
<span class="line"><span class="LineNumber">  16 </span>        <span class="Comment"># create the list control</span></span>
<span class="line"><span class="LineNumber">  17 </span><span class="Comment"></span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">ListCtrl</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">  18 </span>                                <span class="ID">style</span><span class="Operator">=</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">LC_SMALL_ICON</span></span>
<span class="line"><span class="LineNumber">  19 </span>                                <span class="Operator">|</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">LC_AUTOARRANGE</span></span>
<span class="line"><span class="LineNumber">  20 </span>                                <span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  21 </span></span>
<span class="line"><span class="LineNumber">  22 </span>        <span class="Comment"># assign the image list to it</span></span>
<span class="line"><span class="LineNumber">  23 </span><span class="Comment"></span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">AssignImageList</span><span class="Operator">(</span><span class="ID">il</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">IMAGE_LIST_SMALL</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  24 </span></span>
<span class="line"><span class="LineNumber">  25 </span>        <span class="Comment"># create some items for the list</span></span>
<span class="line"><span class="LineNumber">  26 </span><span class="Comment"></span>        <span class="ResWord">for</span> <span class="ID">x</span> <span class="ResWord">in</span> <span class="ID">range</span><span class="Operator">(</span><span class="Number">25</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  27 </span>            <span class="ID">img</span> <span class="Operator">=</span> <span class="ID">x</span> <span class="Operator">%</span> <span class="Operator">(</span><span class="ID">il_max</span><span class="Operator">+</span><span class="Number">1</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  28 </span>            <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">InsertImageStringItem</span><span class="Operator">(</span><span class="ID">x</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">  29 </span>                                            <span class="String">"This is item %02d"</span> <span class="Operator">%</span> <span class="ID">x</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">  30 </span>                                            <span class="ID">img</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  31 </span></span>
<span class="line"><span class="LineNumber">  32 </span><span class="ID">app</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">PySimpleApp</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  33 </span><span class="ID">frame</span> <span class="Operator">=</span> <span class="ID">DemoFrame</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  34 </span><span class="ID">frame</span><span class="Operator">.</span><span class="ID">Show</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  35 </span><span class="ID">app</span><span class="Operator">.</span><span class="ID">MainLoop</span><span class="Operator">(</span><span class="Operator">)</span><span class="Text"></span></span>
</pre>
</div>
<span id="line-148" class="anchor"></span><span id="line-149" class="anchor"></span><p class="line867">
</p>
<h3 id="head-78eac1e2854a52807e2c6092436673f3b7af2eb8">1.3. 什么是列表模式？</h3>
<span id="line-150" class="anchor"></span><span id="line-151" class="anchor"></span><span id="line-152" class="anchor"></span><p class="line874">在列表模式中，列表以多列的形式显示，一列到达底部后自动从下一列的上部继续，如图13.3所示。 <span id="line-153" class="anchor"></span><span id="line-154" class="anchor"></span></p>
<p class="line874">列模式在相同元素的情况下，几乎与小图标模式所能容纳的项目数相同。对这两个模式的选择，主要是根据你的数据是按列组织好呢还是按行组织好。 <span id="line-155" class="anchor"></span><span id="line-156" class="anchor"></span><span id="line-157" class="anchor"></span></p>
<p class="line867"><strong>图13.3</strong> <span id="line-158" class="anchor"></span><span id="line-159" class="anchor"></span></p>
<p class="line867"><img title="attachment:w13.3.gif" src="/moin/WxPythonInAction/ChapterThirteen?action=AttachFile&amp;do=get&amp;target=w13.3.gif" class="attachment"><span id="line-160" class="anchor"></span><span id="line-161" class="anchor"></span><span id="line-162" class="anchor"></span></p>
<p class="line874">产生图13.3的示例代码如下： <span id="line-163" class="anchor"></span><span id="line-164" class="anchor"></span></p>
<p class="line867"><span id="line-165" class="anchor"></span><span id="line-166" class="anchor"></span><span id="line-167" class="anchor"></span><span id="line-168" class="anchor"></span><span id="line-169" class="anchor"></span><span id="line-170" class="anchor"></span><span id="line-171" class="anchor"></span><span id="line-172" class="anchor"></span><span id="line-173" class="anchor"></span><span id="line-174" class="anchor"></span><span id="line-175" class="anchor"></span><span id="line-176" class="anchor"></span><span id="line-177" class="anchor"></span><span id="line-178" class="anchor"></span><span id="line-179" class="anchor"></span><span id="line-180" class="anchor"></span><span id="line-181" class="anchor"></span><span id="line-182" class="anchor"></span><span id="line-183" class="anchor"></span><span id="line-184" class="anchor"></span><span id="line-185" class="anchor"></span><span id="line-186" class="anchor"></span><span id="line-187" class="anchor"></span><span id="line-188" class="anchor"></span><span id="line-189" class="anchor"></span><span id="line-190" class="anchor"></span><span id="line-191" class="anchor"></span><span id="line-192" class="anchor"></span><span id="line-193" class="anchor"></span><span id="line-194" class="anchor"></span><span id="line-195" class="anchor"></span><span id="line-196" class="anchor"></span><span id="line-197" class="anchor"></span><span id="line-198" class="anchor"></span><span id="line-199" class="anchor"></span><span id="line-200" class="anchor"></span></p>
<div lang="en" dir="ltr" class="codearea">
<script type="text/javascript">
document.write('<a href="#" onclick="return togglenumber(\'CA-407cdb94767106769028ffcd6dd329946b48f5bb_002\', 1, 1);" \
class="codenumbers">切换行号显示<\/a>');
</script><a class="codenumbers" onclick="return togglenumber('CA-407cdb94767106769028ffcd6dd329946b48f5bb_002', 1, 1);" href="#">切换行号显示</a>
<pre lang="en" id="CA-407cdb94767106769028ffcd6dd329946b48f5bb_002" dir="ltr"><span class="line"><span class="LineNumber">   1 </span><span class="ResWord">import</span> <span class="ID">wx</span></span>
<span class="line"><span class="LineNumber">   2 </span><span class="ResWord">import</span> <span class="ID">sys</span><span class="Operator">,</span> <span class="ID">glob</span></span>
<span class="line"><span class="LineNumber">   3 </span></span>
<span class="line"><span class="LineNumber">   4 </span><span class="ResWord">class</span> <span class="ID">DemoFrame</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">Frame</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">   5 </span>    <span class="ResWord">def</span> <span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">   6 </span>        <span class="ID">wx</span><span class="Operator">.</span><span class="ID">Frame</span><span class="Operator">.</span><span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">None</span><span class="Operator">,</span> <span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">   7 </span>                          <span class="String">"wx.ListCtrl in wx.LC_LIST mode"</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">   8 </span>                          <span class="ID">size</span><span class="Operator">=</span><span class="Operator">(</span><span class="Number">600</span><span class="Operator">,</span><span class="Number">400</span><span class="Operator">)</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   9 </span></span>
<span class="line"><span class="LineNumber">  10 </span>        <span class="Comment"># load some images into an image list</span></span>
<span class="line"><span class="LineNumber">  11 </span><span class="Comment"></span>        <span class="ID">il</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">ImageList</span><span class="Operator">(</span><span class="Number">16</span><span class="Operator">,</span><span class="Number">16</span><span class="Operator">,</span> <span class="ID">True</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  12 </span>        <span class="ResWord">for</span> <span class="ID">name</span> <span class="ResWord">in</span> <span class="ID">glob</span><span class="Operator">.</span><span class="ID">glob</span><span class="Operator">(</span><span class="String">"smicon??.png"</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  13 </span>            <span class="ID">bmp</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">Bitmap</span><span class="Operator">(</span><span class="ID">name</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">BITMAP_TYPE_PNG</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  14 </span>            <span class="ID">il_max</span> <span class="Operator">=</span> <span class="ID">il</span><span class="Operator">.</span><span class="ID">Add</span><span class="Operator">(</span><span class="ID">bmp</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  15 </span></span>
<span class="line"><span class="LineNumber">  16 </span>        <span class="Comment"># create the list control</span></span>
<span class="line"><span class="LineNumber">  17 </span><span class="Comment"></span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">ListCtrl</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span> <span class="ID">style</span><span class="Operator">=</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">LC_LIST</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  18 </span></span>
<span class="line"><span class="LineNumber">  19 </span>        <span class="Comment"># assign the image list to it</span></span>
<span class="line"><span class="LineNumber">  20 </span><span class="Comment"></span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">AssignImageList</span><span class="Operator">(</span><span class="ID">il</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">IMAGE_LIST_SMALL</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  21 </span></span>
<span class="line"><span class="LineNumber">  22 </span>        <span class="Comment"># create some items for the list</span></span>
<span class="line"><span class="LineNumber">  23 </span><span class="Comment"></span>        <span class="ResWord">for</span> <span class="ID">x</span> <span class="ResWord">in</span> <span class="ID">range</span><span class="Operator">(</span><span class="Number">25</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  24 </span>            <span class="ID">img</span> <span class="Operator">=</span> <span class="ID">x</span> <span class="Operator">%</span> <span class="Operator">(</span><span class="ID">il_max</span><span class="Operator">+</span><span class="Number">1</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  25 </span>            <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">InsertImageStringItem</span><span class="Operator">(</span><span class="ID">x</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">  26 </span>                                            <span class="String">"This is item %02d"</span> <span class="Operator">%</span> <span class="ID">x</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">  27 </span>                                            <span class="ID">img</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  28 </span></span>
<span class="line"><span class="LineNumber">  29 </span></span>
<span class="line"><span class="LineNumber">  30 </span></span>
<span class="line"><span class="LineNumber">  31 </span><span class="ID">app</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">PySimpleApp</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  32 </span><span class="ID">frame</span> <span class="Operator">=</span> <span class="ID">DemoFrame</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  33 </span><span class="ID">frame</span><span class="Operator">.</span><span class="ID">Show</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  34 </span><span class="ID">app</span><span class="Operator">.</span><span class="ID">MainLoop</span><span class="Operator">(</span><span class="Operator">)</span><span class="Text"></span></span>
</pre>
</div>
<span id="line-201" class="anchor"></span><span id="line-202" class="anchor"></span><span id="line-203" class="anchor"></span><p class="line874">在报告模式中，列表显示为真正的多列格式，每行可以有任一数量的列，如图13.4所示。 <span id="line-204" class="anchor"></span><span id="line-205" class="anchor"></span><span id="line-206" class="anchor"></span></p>
<p class="line867">
</p>
<h3 id="head-0734f2e8b8ac6ed6d508b944eb49dcf01fbc7d2f">1.4. 图13.4</h3>
<span id="line-207" class="anchor"></span><span id="line-208" class="anchor"></span><span id="line-209" class="anchor"></span><p class="line867"><img title="attachment:w13.4.gif" src="/moin/WxPythonInAction/ChapterThirteen?action=AttachFile&amp;do=get&amp;target=w13.4.gif" class="attachment"><span id="line-210" class="anchor"></span><span id="line-211" class="anchor"></span><span id="line-212" class="anchor"></span></p>
<p class="line874">报告模式与图标模式不尽相同。例13.2显示了图13.4的代码。 <span id="line-213" class="anchor"></span><span id="line-214" class="anchor"></span><span id="line-215" class="anchor"></span></p>
<p class="line867"><strong>例13.2</strong>  <span id="line-216" class="anchor"></span><strong>创建报告模式的一个列表</strong> <span id="line-217" class="anchor"></span><span id="line-218" class="anchor"></span></p>
<p class="line867"><span id="line-219" class="anchor"></span><span id="line-220" class="anchor"></span><span id="line-221" class="anchor"></span><span id="line-222" class="anchor"></span><span id="line-223" class="anchor"></span><span id="line-224" class="anchor"></span><span id="line-225" class="anchor"></span><span id="line-226" class="anchor"></span><span id="line-227" class="anchor"></span><span id="line-228" class="anchor"></span><span id="line-229" class="anchor"></span><span id="line-230" class="anchor"></span><span id="line-231" class="anchor"></span><span id="line-232" class="anchor"></span><span id="line-233" class="anchor"></span><span id="line-234" class="anchor"></span><span id="line-235" class="anchor"></span><span id="line-236" class="anchor"></span><span id="line-237" class="anchor"></span><span id="line-238" class="anchor"></span><span id="line-239" class="anchor"></span><span id="line-240" class="anchor"></span><span id="line-241" class="anchor"></span><span id="line-242" class="anchor"></span><span id="line-243" class="anchor"></span><span id="line-244" class="anchor"></span><span id="line-245" class="anchor"></span><span id="line-246" class="anchor"></span><span id="line-247" class="anchor"></span><span id="line-248" class="anchor"></span><span id="line-249" class="anchor"></span><span id="line-250" class="anchor"></span><span id="line-251" class="anchor"></span><span id="line-252" class="anchor"></span><span id="line-253" class="anchor"></span><span id="line-254" class="anchor"></span><span id="line-255" class="anchor"></span><span id="line-256" class="anchor"></span><span id="line-257" class="anchor"></span><span id="line-258" class="anchor"></span><span id="line-259" class="anchor"></span><span id="line-260" class="anchor"></span><span id="line-261" class="anchor"></span><span id="line-262" class="anchor"></span><span id="line-263" class="anchor"></span><span id="line-264" class="anchor"></span></p>
<div lang="en" dir="ltr" class="codearea">
<script type="text/javascript">
document.write('<a href="#" onclick="return togglenumber(\'CA-db1a2899b5fcd5a03d9db18182b922fd8f793ac6_003\', 1, 1);" \
class="codenumbers">切换行号显示<\/a>');
</script><a class="codenumbers" onclick="return togglenumber('CA-db1a2899b5fcd5a03d9db18182b922fd8f793ac6_003', 1, 1);" href="#">切换行号显示</a>
<pre lang="en" id="CA-db1a2899b5fcd5a03d9db18182b922fd8f793ac6_003" dir="ltr"><span class="line"><span class="LineNumber">   1 </span><span class="Comment">#!/usr/bin/python</span></span>
<span class="line"><span class="LineNumber">   2 </span><span class="Comment"></span><span class="Comment">#-*- encoding:UTF-8 -*-</span></span>
<span class="line"><span class="LineNumber">   3 </span><span class="Comment"></span><span class="ResWord">import</span> <span class="ID">wx</span></span>
<span class="line"><span class="LineNumber">   4 </span><span class="ResWord">import</span> <span class="ID">sys</span><span class="Operator">,</span> <span class="ID">glob</span><span class="Operator">,</span> <span class="ID">random</span></span>
<span class="line"><span class="LineNumber">   5 </span><span class="ResWord">import</span> <span class="ID">data</span></span>
<span class="line"><span class="LineNumber">   6 </span></span>
<span class="line"><span class="LineNumber">   7 </span><span class="ResWord">class</span> <span class="ID">DemoFrame</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">Frame</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">   8 </span>    <span class="ResWord">def</span> <span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">   9 </span>        <span class="ID">wx</span><span class="Operator">.</span><span class="ID">Frame</span><span class="Operator">.</span><span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">None</span><span class="Operator">,</span> <span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">  10 </span>                          <span class="String">"wx.ListCtrl in wx.LC_REPORT mode"</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">  11 </span>                          <span class="ID">size</span><span class="Operator">=</span><span class="Operator">(</span><span class="Number">600</span><span class="Operator">,</span><span class="Number">400</span><span class="Operator">)</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  12 </span></span>
<span class="line"><span class="LineNumber">  13 </span>        <span class="ID">il</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">ImageList</span><span class="Operator">(</span><span class="Number">16</span><span class="Operator">,</span><span class="Number">16</span><span class="Operator">,</span> <span class="ID">True</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  14 </span>        <span class="ResWord">for</span> <span class="ID">name</span> <span class="ResWord">in</span> <span class="ID">glob</span><span class="Operator">.</span><span class="ID">glob</span><span class="Operator">(</span><span class="String">"smicon??.png"</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  15 </span>            <span class="ID">bmp</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">Bitmap</span><span class="Operator">(</span><span class="ID">name</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">BITMAP_TYPE_PNG</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  16 </span>            <span class="ID">il_max</span> <span class="Operator">=</span> <span class="ID">il</span><span class="Operator">.</span><span class="ID">Add</span><span class="Operator">(</span><span class="ID">bmp</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  17 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">ListCtrl</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span> <span class="ID">style</span><span class="Operator">=</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">LC_REPORT</span><span class="Operator">)</span><span class="Comment">#创建列表</span></span>
<span class="line"><span class="LineNumber">  18 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">AssignImageList</span><span class="Operator">(</span><span class="ID">il</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">IMAGE_LIST_SMALL</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  19 </span></span>
<span class="line"><span class="LineNumber">  20 </span>        <span class="Comment"># Add some columns</span></span>
<span class="line"><span class="LineNumber">  21 </span><span class="Comment"></span>        <span class="ResWord">for</span> <span class="ID">col</span><span class="Operator">,</span> <span class="ID">text</span> <span class="ResWord">in</span> <span class="ID">enumerate</span><span class="Operator">(</span><span class="ID">data</span><span class="Operator">.</span><span class="ID">columns</span><span class="Operator">)</span><span class="Operator">:</span><span class="Comment">#增加列</span></span>
<span class="line"><span class="LineNumber">  22 </span>            <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">InsertColumn</span><span class="Operator">(</span><span class="ID">col</span><span class="Operator">,</span> <span class="ID">text</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  23 </span></span>
<span class="line"><span class="LineNumber">  24 </span>        <span class="Comment"># add the rows</span></span>
<span class="line"><span class="LineNumber">  25 </span><span class="Comment"></span>        <span class="ResWord">for</span> <span class="ID">item</span> <span class="ResWord">in</span> <span class="ID">data</span><span class="Operator">.</span><span class="ID">rows</span><span class="Operator">:</span><span class="Comment">#增加行</span></span>
<span class="line"><span class="LineNumber">  26 </span>            <span class="ID">index</span> <span class="Operator">=</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">InsertStringItem</span><span class="Operator">(</span><span class="ID">sys</span><span class="Operator">.</span><span class="ID">maxint</span><span class="Operator">,</span> <span class="ID">item</span><span class="Operator">[</span><span class="Number">0</span><span class="Operator">]</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  27 </span>            <span class="ResWord">for</span> <span class="ID">col</span><span class="Operator">,</span> <span class="ID">text</span> <span class="ResWord">in</span> <span class="ID">enumerate</span><span class="Operator">(</span><span class="ID">item</span><span class="Operator">[</span><span class="Number">1</span><span class="Operator">:</span><span class="Operator">]</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  28 </span>                <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">SetStringItem</span><span class="Operator">(</span><span class="ID">index</span><span class="Operator">,</span> <span class="ID">col</span><span class="Operator">+</span><span class="Number">1</span><span class="Operator">,</span> <span class="ID">text</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  29 </span></span>
<span class="line"><span class="LineNumber">  30 </span>            <span class="Comment"># give each item a random image</span></span>
<span class="line"><span class="LineNumber">  31 </span><span class="Comment"></span>            <span class="ID">img</span> <span class="Operator">=</span> <span class="ID">random</span><span class="Operator">.</span><span class="ID">randint</span><span class="Operator">(</span><span class="Number">0</span><span class="Operator">,</span> <span class="ID">il_max</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  32 </span>            <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">SetItemImage</span><span class="Operator">(</span><span class="ID">index</span><span class="Operator">,</span> <span class="ID">img</span><span class="Operator">,</span> <span class="ID">img</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  33 </span></span>
<span class="line"><span class="LineNumber">  34 </span>        <span class="Comment"># set the width of the columns in various ways</span></span>
<span class="line"><span class="LineNumber">  35 </span><span class="Comment"></span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">SetColumnWidth</span><span class="Operator">(</span><span class="Number">0</span><span class="Operator">,</span> <span class="Number">120</span><span class="Operator">)</span><span class="Comment">#设置列的宽度</span></span>
<span class="line"><span class="LineNumber">  36 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">SetColumnWidth</span><span class="Operator">(</span><span class="Number">1</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">LIST_AUTOSIZE</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  37 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">SetColumnWidth</span><span class="Operator">(</span><span class="Number">2</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">LIST_AUTOSIZE</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  38 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">SetColumnWidth</span><span class="Operator">(</span><span class="Number">3</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">LIST_AUTOSIZE_USEHEADER</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  39 </span></span>
<span class="line"><span class="LineNumber">  40 </span></span>
<span class="line"><span class="LineNumber">  41 </span><span class="ID">app</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">PySimpleApp</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  42 </span><span class="ID">frame</span> <span class="Operator">=</span> <span class="ID">DemoFrame</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  43 </span><span class="ID">frame</span><span class="Operator">.</span><span class="ID">Show</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  44 </span><span class="ID">app</span><span class="Operator">.</span><span class="ID">MainLoop</span><span class="Operator">(</span><span class="Operator">)</span><span class="Text"></span></span>
</pre>
</div>
<span id="line-265" class="anchor"></span><span id="line-266" class="anchor"></span><p class="line862">注意：如果代码中有中文或中文注释，那么请在代码开头加上#-*- <tt>encoding:UTF</tt>-8 -*- <span id="line-267" class="anchor"></span><span id="line-268" class="anchor"></span></p>
<p class="line862">在接下来的部分，我们将讨论如何将值插入适当的位置。报告控件是最适合用于那些包含一两个附加的数据列的简单列表，它的显示逻辑没有打算做得很复杂。如果你的列表控件复杂的话，或包含更多的数据的话，那么建议你使用<tt>grid</tt>控件，说明见第14章。 <span id="line-269" class="anchor"></span><span id="line-270" class="anchor"></span><span id="line-271" class="anchor"></span></p>
<p class="line867">
</p>
<h3 id="head-a19a85d7b71ad8fd110b9530440b6ac1e1780638">1.5. 如何创建一个列表控件？</h3>
<span id="line-272" class="anchor"></span><span id="line-273" class="anchor"></span><span id="line-274" class="anchor"></span><p class="line862">一个<tt>wxPython</tt>列表控件是类<tt>wx.ListCtrl</tt>的一个实例。它的构造函数与其它的窗口部件的构造函数相似： <span id="line-275" class="anchor"></span><span id="line-276" class="anchor"></span></p>
<p class="line867"><tt>wx.ListCtrl(parent, id, pos=wx.DefaultPosition, </tt> <span id="line-277" class="anchor"></span></p>
<ul><li style="list-style-type: none;"><p class="line891"><tt>size=wx.DefaultSize, style=wx.LC_ICON, </tt> <span id="line-278" class="anchor"></span><tt>validator=wx.DefaultValidator, name=</tt>"<tt>listCtrl</tt>") <span id="line-279" class="anchor"></span><span id="line-280" class="anchor"></span></p></li></ul>
<p class="line862">这些参数我们在其它的窗口部件的构造函数中见过。参数<tt>parent</tt>是容器部件，<tt>id</tt>是<tt>wxPython</tt>标识符，使用-1表明自动创建标识符。具体的布局由参数<tt>pos</tt>和<tt>size</tt>来管理。<tt>style</tt>控制模式和其它的显示方案——贯穿本章，我们都将看到这些值。参数<tt>validator</tt>用于验证特定的输入，我们在第9章讨论过。参数<tt>name</tt>我们很少使用。 <span id="line-281" class="anchor"></span><span id="line-282" class="anchor"></span></p>
<p class="line862">样式（<tt>style</tt>）标记是一个位掩码，它管理列表控件的一些不同的特定。样式标记的第一组值用于设置列表的显示模式。默认模式是<tt>wx.LC_ICON</tt>。表13.1显示了列表控件的模式值。 <span id="line-283" class="anchor"></span><span id="line-284" class="anchor"></span><span id="line-285" class="anchor"></span></p>
<p class="line867"><strong>表13.1</strong>  <span id="line-286" class="anchor"></span><strong>列表控件模式值</strong> <span id="line-287" class="anchor"></span><span id="line-288" class="anchor"></span></p>
<div><table><tbody>
<tr>
<td><p class="line891"><tt>wx.LC_ICON</tt></p></td>
<td><p class="line862">图标模式，使用大图标</p></td>
</tr>
<tr>
<td>
<span id="line-289" class="anchor"></span><p class="line891"><tt>wx.LC_LIST</tt></p>
</td>
<td><p class="line862">列表模式</p></td>
</tr>
<tr>
<td>
<span id="line-290" class="anchor"></span><p class="line891"><tt>wx.LC_REPORT</tt></p>
</td>
<td><p class="line862">报告模式</p></td>
</tr>
<tr>
<td>
<span id="line-291" class="anchor"></span><p class="line891"><tt>wx.LC_SMALL_ICON</tt></p>
</td>
<td><p class="line862">图标模式，使用小图标</p></td>
</tr>
</tbody></table></div>
<span id="line-292" class="anchor"></span><span id="line-293" class="anchor"></span><p class="line862">在图标或小图标列表中，有三个样式标记用来控件图标相对于列表对齐的。默认值是<tt>wx.LC_ALIGN_TOP</tt>，它按列表的顶部对齐。要左对齐的话，使用<tt>wx.LC_ALIGN_LEFT</tt>。样式<tt>LC_AUTOARRANGE</tt>使得当图标排列到达窗口右或底边时自动换行或换列。 <span id="line-294" class="anchor"></span><span id="line-295" class="anchor"></span></p>
<p class="line874">表13.2显示了作用于报告列表显示的样式。 <span id="line-296" class="anchor"></span><span id="line-297" class="anchor"></span><span id="line-298" class="anchor"></span></p>
<p class="line867"><strong>表13.2</strong>  <span id="line-299" class="anchor"></span><strong>报告列表的显示样式</strong> <span id="line-300" class="anchor"></span><span id="line-301" class="anchor"></span></p>
<div><table><tbody>
<tr>
<td><p class="line891"><tt>wx.LC_HRULES</tt></p></td>
<td><p class="line862">在列表的行与行间显示网格线（水平分隔线）</p></td>
</tr>
<tr>
<td>
<span id="line-302" class="anchor"></span><p class="line891"><tt>wx.LC_NO_HEADER</tt></p>
</td>
<td><p class="line862">不显示列标题</p></td>
</tr>
<tr>
<td>
<span id="line-303" class="anchor"></span><p class="line891"><tt>wx.LC_VRULES</tt></p>
</td>
<td><p class="line862">显示列与列之间的网格线（竖直分隔线）</p></td>
</tr>
</tbody></table></div>
<span id="line-304" class="anchor"></span><span id="line-305" class="anchor"></span><p class="line862">样式标记可以通过位运算符来组合。使用<tt>wx.LC_REPORT</tt>|<tt>wx.LC_HRULES</tt>|<tt>wx.LC_VRULES</tt>组合可以得到一个非常像网格的一个列表。默认情况下，所有的列表控件都允许多选。要使得一次只能选列表中的一个项目，可以使用标记<tt>wx.LC_SINGLE_SEL</tt>。 <span id="line-306" class="anchor"></span><span id="line-307" class="anchor"></span></p>
<p class="line862">与我们见过的其它的窗口部件不同，列表控件增加了一对用于在运行时改变已存在的列表控件的样式标记的方法。<tt>SetSingleStyle(style, add=True)</tt>方法使你能够增加或去掉一个样式标记，这依赖于参数<tt>add</tt>的值。<tt>listCtrl.SetSingleStyle(LC_HRULES,True)</tt>将增加水平分隔线，而<tt>listCtrl.SetSingleStyle(LC_HRULES,False)</tt>将去掉水平分隔线。<tt>listCtrl</tt>代表具体的列表控件。<tt>SetWindowStyleFlag(style)</tt>能够重置整个窗口的样式，如<tt>SetWindowStyleFlag(LC_REPORT </tt>|
<tt>LC_NO_HEADER)</tt>。这些方法对于在运行时修改列表控件的样式就有用处的。 <span id="line-308" class="anchor"></span><span id="line-309" class="anchor"></span><span id="line-310" class="anchor"></span></p>
<p class="line867">
</p>
<h2 id="head-b5bca580d3b23c4a4fc37fcddc3ea0c2170c5460">2. 处理列表中的项目</h2>
<span id="line-311" class="anchor"></span><span id="line-312" class="anchor"></span><span id="line-313" class="anchor"></span><p class="line862">一旦列表控件被创建，你就能够开始将信息添加到该列表中。在<tt>wxPython</tt>中，对于纯文本信息和对与列表中的每个项目相关的图像的处理是不同的。在接下来的几节里，我们将如何添加图像和文本到你的列表控件中。 <span id="line-314" class="anchor"></span><span id="line-315" class="anchor"></span><span id="line-316" class="anchor"></span></p>
<p class="line867">
</p>
<h3 id="head-c0f7c2e46fd4fc667c0838aa8ddc2fcfe23ef86b">2.1. 什么是一个图像列表以及如何将图像添加给它？</h3>
<span id="line-317" class="anchor"></span><span id="line-318" class="anchor"></span><span id="line-319" class="anchor"></span><p class="line862">在我们讨论信息是如何被添加到列表控件之前，我们需要对列表如何控制图像说两句。任何使用在一个列表控件中的图像，首先必须被添加到一个图像列表，图像列表是一个图像索引数组，使用列表控件存储。当一个图像与列表中的一个特定项目相关联时，图像列表中的该图像的索引被用来引用该图像，而非使用图像本身。该机制确保每个图像只被装载一次。这是为了在一个图标被列表中的几个项目重复使用时节约内存。它也允许相同图像的多个版本之间的相对直接的连接，这些版本被用来表示不同的模式。关于创建<tt>wxPython</tt>图像和位图的更多的信息，请看第12章。 <span id="line-320" class="anchor"></span><span id="line-321" class="anchor"></span><span id="line-322" class="anchor"></span></p>
<p class="line867"><strong>创建一个图像列表</strong> <span id="line-323" class="anchor"></span><span id="line-324" class="anchor"></span></p>
<p class="line862">图像列表是<tt>wx.ImageList</tt>的一个实例，构造函数如下： <span id="line-325" class="anchor"></span><span id="line-326" class="anchor"></span></p>
<p class="line867"><tt>wx.ImageList(width, height, mask=True, initialCount=1) </tt> <span id="line-327" class="anchor"></span><span id="line-328" class="anchor"></span></p>
<p class="line862">参数<tt>width</tt>和<tt>height</tt>指定了添加到列表中的图像的像素尺寸。比指定大小大的图像是不允许的。参数<tt>mask</tt>是一个布尔值。如果为<tt>True</tt>，假如图像有遮罩，则使用遮罩绘制图像。参数<tt>initialCount</tt>设置列表的初始的内在尺寸。如果你知道列表会很大，那么指定初始量可以获得更多的内存分配以便稍后使用。 <span id="line-329" class="anchor"></span><span id="line-330" class="anchor"></span><span id="line-331" class="anchor"></span></p>
<p class="line867"><strong>添加及移去图像</strong> <span id="line-332" class="anchor"></span><span id="line-333" class="anchor"></span></p>
<p class="line862">你可以使用方法<tt>Add(bitmap,
mask=wx.NullBitmap)</tt>来将一个图像添加到列表，参数<tt>bitmap</tt>和<tt>mask</tt>都是<tt>wx.Bitmap</tt>的实例。<tt>mask</tt>参数是一个单色位图，它代表该图像的透明部分，如果指定了<tt>mask</tt>参数的话。如果位图已经有一个与之相关的遮罩，那么该遮罩被默认使用。如果位图没有一个遮罩，并且你不使用单色透明映射，但设置了该位图的一个特定颜色作为这个透明色的话，那么你可以使用<tt>AddWithColourMask(bitmap</tt>，<tt>colour)</tt>方法，其中参数<tt>colour</tt>是用作遮罩的<tt>wxPython</tt>颜色（或它的颜色名）。如果你有一个<tt>wx.Icon</tt>对象要添加到图像列表，可以使用方法<tt>AddIcon(icon)</tt>。所有这些添加方法都返回这个新加的图像在列表中的索引值，你可以保留索引值以便日后使用该图像。
<span id="line-334" class="anchor"></span><span id="line-335" class="anchor"></span></p>
<p class="line874">下面的代码片断显示了一个创建图像列表的例子（类似于例13.1中的）。 <span id="line-336" class="anchor"></span><span id="line-337" class="anchor"></span><span id="line-338" class="anchor"></span><span id="line-339" class="anchor"></span><span id="line-340" class="anchor"></span><span id="line-341" class="anchor"></span></p>
<div lang="en" dir="ltr" class="codearea">
<script type="text/javascript">
document.write('<a href="#" onclick="return togglenumber(\'CA-0b41ef7ba09821a249484a5afa0013020f69ded7_004\', 1, 1);" \
class="codenumbers">切换行号显示<\/a>');
</script><a class="codenumbers" onclick="return togglenumber('CA-0b41ef7ba09821a249484a5afa0013020f69ded7_004', 1, 1);" href="#">切换行号显示</a>
<pre lang="en" id="CA-0b41ef7ba09821a249484a5afa0013020f69ded7_004" dir="ltr"><span class="line"><span class="LineNumber">   1 </span><span class="ID">il</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">ImageList</span><span class="Operator">(</span><span class="Number">32</span><span class="Operator">,</span> <span class="Number">32</span><span class="Operator">,</span> <span class="ID">True</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   2 </span><span class="ResWord">for</span> <span class="ID">name</span> <span class="ResWord">in</span> <span class="ID">glob</span><span class="Operator">.</span><span class="ID">glob</span><span class="Operator">(</span><span class="String">"icon??.png"</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">   3 </span>    <span class="ID">bmp</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">Bitmap</span><span class="Operator">(</span><span class="ID">name</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">BITMAP_TYPE_PNG</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   4 </span>    <span class="ID">il_max</span> <span class="Operator">=</span> <span class="ID">il</span><span class="Operator">.</span><span class="ID">Add</span><span class="Operator">(</span><span class="ID">bmp</span><span class="Operator">)</span><span class="Text"></span></span>
</pre>
</div>
<span id="line-342" class="anchor"></span><p class="line874">然后这个图像列表必须被赋给一个列表控件，使用下面的方法： <span id="line-343" class="anchor"></span><span id="line-344" class="anchor"></span></p>
<p class="line867"><tt>self.list.AssignImageList(il, wx.IMAGE_LIST_NORMAL) </tt> <span id="line-345" class="anchor"></span><span id="line-346" class="anchor"></span></p>
<p class="line862">要从图像列表删除一个图像，可以使用<tt>Remove(index)</tt>方法，其中的<tt>index</tt>是图像在图像列表中的整数索引值。这个方法会修删除点之后的图像在图像列表中的索引值，如果在你的程序中有对特定的索引存在依赖关系的话，这可能会导致一些问题。要删除整个图像列表，使用<tt>RemoveAll()</tt>。你可以使用方法<tt>Replace(index,  bitmap,
mask=wx.NullBitmap)</tt>修改特定索引相关的位图，其中<tt>index</tt>是列表中要修改处的索引，<tt>bitmap</tt>和<tt>mask</tt>与<tt>Add()</tt>方法中的一样。如果要修改的项目是一个图标，可以使用方法<tt>ReplaceIcon(index, icon)</tt>。这里没有处理颜色遮罩的替换方法。 <span id="line-347" class="anchor"></span><span id="line-348" class="anchor"></span><span id="line-349" class="anchor"></span></p>
<p class="line867"><strong>使用图像列表</strong> <span id="line-350" class="anchor"></span><span id="line-351" class="anchor"></span></p>
<p class="line862">通过使用方法<tt>GetImageCount()</tt>，你能够得到图像列表的长度，使用<tt>GetSize()</tt>方法，你可以得到其中个个图像的尺寸，它返回一个(<tt>width, height)</tt>元组。 <span id="line-352" class="anchor"></span><span id="line-353" class="anchor"></span></p>
<p class="line862">在列表控件上下文中没有直接相关的图像的时候，你也可以根据图像列表绘制一个图像到设备上下文中。关于设备上下文的更多信息，请看第6章和第12章。这个方法是<tt>Draw</tt>，如下所示： <span id="line-354" class="anchor"></span><span id="line-355" class="anchor"></span><span id="line-356" class="anchor"></span><span id="line-357" class="anchor"></span></p>
<div lang="en" dir="ltr" class="codearea">
<script type="text/javascript">
document.write('<a href="#" onclick="return togglenumber(\'CA-b1d0babb5d6c597bd168d15aff15dfced103b18a_005\', 1, 1);" \
class="codenumbers">切换行号显示<\/a>');
</script><a class="codenumbers" onclick="return togglenumber('CA-b1d0babb5d6c597bd168d15aff15dfced103b18a_005', 1, 1);" href="#">切换行号显示</a>
<pre lang="en" id="CA-b1d0babb5d6c597bd168d15aff15dfced103b18a_005" dir="ltr"><span class="line"><span class="LineNumber">   1 </span><span class="ID">Draw</span><span class="Operator">(</span><span class="ID">index</span><span class="Operator">,</span> <span class="ID">dc</span><span class="Operator">,</span> <span class="ID">x</span><span class="Operator">,</span> <span class="ID">y</span><span class="Operator">,</span> <span class="ID">flags</span><span class="Operator">=</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">IMAGELIST_DRAW_NORMAL</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">   2 </span>        <span class="ID">solidBackground</span><span class="Operator">=</span><span class="ID">False</span><span class="Operator">)</span><span class="Text"></span></span>
</pre>
</div>
<span id="line-358" class="anchor"></span><p class="line862">在这个调用中，参数<tt>index</tt>是要绘制的项目在图像列表中的索引，参数<tt>dc</tt>是要绘制到的一个<tt>wx.DC</tt>设备上下文。<tt>flags</tt>控制图像被如何绘制，<tt>flags</tt>的可取值有<tt>wx.IMAGELIST_DRAW_NORMAL,  wx.IMAGELIST_DRAW_TRANSPARENT,  wx.IMAGELISTDRAW_SelectED, </tt>和 <tt>wx.IMAGELIST_DRAW_FOCUSED</tt>。如果<tt>solidBackground</tt>为<tt>True</tt>，那么该绘制方法使用一个更快的算法工作。 <span id="line-359" class="anchor"></span><span id="line-360" class="anchor"></span></p>
<p class="line862">一旦你有了一个图像列表，你就需要将它附给列表控件。这个以通过后面的任一个方法来实现：<tt>AssignImage(imageList, which)</tt>或<tt>SetImage(imageList, which)</tt>。<tt>imageList</tt>参数是一个图像列表，参数<tt>which</tt>是标记值：<tt>wx.IMAGE_LIST_NORMAL </tt>或
<tt>wx.IMAGE_LIST_SMALL</tt>。这两个方法的唯一的不同之处是C++对图像列表的处理方面。对于<tt>AssignImage()</tt>，图像列表变成了列表控件的一部分，并随列表控件的销毁而销毁。对于<tt>SetImage()</tt>，图像列表有自己的生命周期，当列表控件被销毁时不被自动处理，只是当其<tt>Python</tt>对象退出作用域时，才被处理。 <span id="line-361" class="anchor"></span><span id="line-362" class="anchor"></span></p>
<p class="line862">可以赋给列表控件两个图像列表。普通的图像列表（使用了<tt>wx.IMAGE_LIST_NORMAL</tt>）被用于标准的图标模式。小图像列表（使用了<tt>wx.IMAGE_LIST_SMALL</tt>）被用于报告和小图标模式。在大多数情况下，你只需要一个图像列表，但是如果你希望列表以多模式显示（这样用户可以从普通模式切换到小图标模式），那么你应该两个都提供。如果你这样做了，那么记住，列表控件中的选项将只会经由图像列表中的索引知道相关的图像。如果文档图标在普通尺寸的图像列表中有两个索引，那么也必须在小图像列表中有两个索引。 <span id="line-363" class="anchor"></span><span id="line-364" class="anchor"></span></p>
<p class="line862">关于列表控件还有一个相关的<tt>get</tt>*方法：<tt>GetImageList(which)</tt>，它返回与<tt>which</tt>标记参数相关的图像列表。 <span id="line-365" class="anchor"></span><span id="line-366" class="anchor"></span><span id="line-367" class="anchor"></span></p>
<p class="line867">
</p>
<h3 id="head-d62685c29e7f5fb1867e84f2b37864aa7ff798b0">2.2. 如何对一个列表添加或删除项目？</h3>
<span id="line-368" class="anchor"></span><span id="line-369" class="anchor"></span><span id="line-370" class="anchor"></span><p class="line862">在你能显示一个列表之前，你需要给它增加文本信息。在一个图标列表中，你可以增加新的项目如图标、字符串或两个都添加。在一个报告视图中，你也可以在设置了初始图标和/或字符串后，为一行中的不同的列设置信息。用于处理列表控件项目的方法的<tt>API</tt>及其命名习惯与迄今为止我们所见过的其它一些控件的是有区别的，因此，尽管你已经理解了菜单或列表框是如何工作的，但是你仍将需要读这一节。 <span id="line-371" class="anchor"></span><span id="line-372" class="anchor"></span></p>
<p class="line874">对于一个图标列表，增加文本信息到列表控件是一个单步的处理过程，但是对于一个报告列表就需要多步才行。通常对于每个列表，第一步是在行中增加第一个项目。对于报告列表，你必须分别地增加列和列中的信息，而非最左边的一个。 <span id="line-373" class="anchor"></span><span id="line-374" class="anchor"></span><span id="line-375" class="anchor"></span></p>
<p class="line867"><strong>增加一个新行</strong> <span id="line-376" class="anchor"></span><span id="line-377" class="anchor"></span></p>
<p class="line862">要增加一个新行，使用<tt>InsertItem()</tt>这类的一种方法。具体所用的方法依赖于你所插入的项目的类型。如果你仅仅插入一个字符串到列表中，使用<tt>InsertStringItem(index, label)</tt>，其中的<tt>index</tt>是要插入并显示新项目的行的索引。如果你只插入一个图像，那么使用<tt>InsertImageItem(index,
imageIndex)</tt>。在这种情况下，这<tt>index</tt>是要插入图像的行的索引，<tt>imageIndex</tt>是附加到该列表控件的图像列表中的图像的索引。要插入一个图像项目，图像列表必须已经被创建并赋值。如果你使用的图像索引超出了图像列表的边界，那么你将得到一个空图像。如果你想增加一个既有图像又有字符串标签的项目，使用<tt>InsertImageStringItem(index, label, imageIndex)</tt>。这个方法综合了前面两个方法的参数，参数的意义不变。 <span id="line-378" class="anchor"></span><span id="line-379" class="anchor"></span></p>
<p class="line862">在内部，列表控件使用类<tt>wx.ListItem</tt>的实例来管理有关它的项目的信息。我还要说的是，最后一种插入项目到列表控件中方法是<tt>InsertItem(index, item)</tt>，其中的<tt>item</tt>是<tt>wx.ListItem</tt>的一个实例。对于<tt>wx.ListItem</tt>，这里我们不将做很详细的说明，这是因为你几乎不会用到它并且该类也不很复杂——它几乎都是由<tt>get</tt>*和<tt>set</tt>*方法组成的。一个列表项的几乎所有属性都可通过列表控件的方法来访问。 <span id="line-380" class="anchor"></span><span id="line-381" class="anchor"></span><span id="line-382" class="anchor"></span></p>
<p class="line867"><strong>增加列</strong> <span id="line-383" class="anchor"></span><span id="line-384" class="anchor"></span></p>
<p class="line862">要增加报告模式的列表控件的列，先要创建列，然后设置每行/列对的单独的数据单元格。使用<tt>InsertColumn()</tt>方法创建列，它的语法如下： <span id="line-385" class="anchor"></span><span id="line-386" class="anchor"></span></p>
<p class="line867"><span id="line-387" class="anchor"></span></p>
<pre>InsertColumn(col, heading, format=wx.LIST_FORMAT_LEFT, width=-1)
<span id="line-388" class="anchor"></span></pre>
<span id="line-389" class="anchor"></span><span id="line-390" class="anchor"></span><p class="line862">在这个方法中，参数<tt>col</tt>是列表中的新列的索引，你必须提供这个值。参数<tt>heading</tt>是列标题。参数<tt>format</tt>控件列中文本的对齐方式，取值有：<tt>wx.LIST_FORMAT_CENTRE</tt>、<tt>wx.LIST_FORMAT_LEFT</tt>、和 <tt>wx.LIST_FORMAT_RIGHT</tt>。 <span id="line-391" class="anchor"></span>参数<tt>width</tt>是列的初始显示宽度（像素单位）——用户可以通过拖动列的头部的边来改变它的宽度。要使用一个<tt>wx.ListItem</tt>对象来设置列的话，也有一个名为<tt>InsertColumnInfo(info)</tt>的方法，它要求一个列表项作为参数。 <span id="line-392" class="anchor"></span><span id="line-393" class="anchor"></span><span id="line-394" class="anchor"></span></p>
<p class="line867"><strong>设置多列列表中的值</strong> <span id="line-395" class="anchor"></span><span id="line-396" class="anchor"></span></p>
<p class="line862">你可能已经注意到使用前面说明的行的方法来插入项目，对于一个多列的报告列表来说只能设置最初的那列。要在另外的列中设置字符串，可以使用方法<tt>SetStringItem()</tt>。 <span id="line-397" class="anchor"></span><span id="line-398" class="anchor"></span></p>
<p class="line867"><span id="line-399" class="anchor"></span></p>
<pre>SetStringItem(index, col, label, imageId=-1)
<span id="line-400" class="anchor"></span></pre>
<span id="line-401" class="anchor"></span><span id="line-402" class="anchor"></span><p class="line862">参数<tt>index</tt>和<tt>col</tt>是你要设置的单元格的行和列的索引。你可以设定<tt>col</tt>为0来设置第一列，但是参数<tt>index</tt>必须对应列表控件中已有的行——换句话说，这个方法只能对已有的行使用。参数<tt>label</tt>是显示在单元格中文本，参数<tt>imageId</tt>是图像列表中的索引（如果你想在单元格中显示一个图像的话可以设置这个参数）。 <span id="line-403" class="anchor"></span><span id="line-404" class="anchor"></span></p>
<p class="line867"><tt>SetStringItem()</tt>是<tt>SetItem(info)</tt>方法的一种特殊情况，<tt>SetItem(info)</tt>方法要求一个<tt>wx.ListItem</tt>实例。要使用这个方法，在将<tt>wx.ListItem</tt>实例增加到一个列表之前，要先设置它行，列和其它的参数。你也可以使用<tt>GetItem(index,col=0)</tt>方法来得到单元格处的<tt>wx.ListItem</tt>实例，默认情况下，该方法返回一行的第一列，你可以通过设置参数<tt>col</tt>来选择其它列的一项。 <span id="line-405" class="anchor"></span><span id="line-406" class="anchor"></span><span id="line-407" class="anchor"></span></p>
<p class="line867"><strong>项目属性</strong> <span id="line-408" class="anchor"></span><span id="line-409" class="anchor"></span></p>
<p class="line862">有许多的<tt>get</tt>*和<tt>set</tt>*方法使你能够指定部分项目。通常这些方法工作在一行的第一列上。要得工作在其它的列上，你需要使用<tt>GetItem()</tt>来得到项目，并使用项目类的<tt>get</tt>*和<tt>set</tt>*方法。你可以使用<tt>SetItemImage(item, image,
selImage)</tt>来为一个项目设置图像，其中的<tt>item</tt>参数是该项目在列表中的索引，<tt>image</tt>和<tt>selImage</tt>都是图像列表中的索引，分别代表通常显示的图像和被选中时显示的图像。你可以通过使用<tt>GetItemText(item)</tt>和<tt>SetItemText(item,text)</tt>方法来得到或设置一个项目的文本。 <span id="line-410" class="anchor"></span><span id="line-411" class="anchor"></span></p>
<p class="line862">你可以使用<tt>GetItemState(item,stateMask)</tt>和<tt>SetItemState(item, state,
stateMask)</tt>来得到或设置单独一个项目的状态。<tt>state</tt>和<tt>stateMask</tt>的取值见表13.3。参数<tt>state</tt>（及<tt>GetItemState</tt>的返回值）是项目的实际状态，<tt>stateMask</tt>是当前关注的所有可能值的一个掩码。 <span id="line-412" class="anchor"></span><span id="line-413" class="anchor"></span></p>
<p class="line862">你可以使用<tt>GetColumn(col)</tt>来得到一个指定的列，它返回索引<tt>col</tt>处的列的<tt>wx.ListItem</tt>实例。 <span id="line-414" class="anchor"></span><span id="line-415" class="anchor"></span><span id="line-416" class="anchor"></span></p>
<p class="line867"><strong>表13.3</strong>  <span id="line-417" class="anchor"></span><strong>状态掩码参数</strong> <span id="line-418" class="anchor"></span><span id="line-419" class="anchor"></span></p>
<p class="line874">状态及说明如下： <span id="line-420" class="anchor"></span><span id="line-421" class="anchor"></span></p>
<div><table><tbody>
<tr>
<td><p class="line891"><tt>wx.LIST_STATE_CUT</tt></p></td>
<td><p class="line862">被剪切状态。这个状态只在微软<tt>Windows</tt>下有效。</p></td>
</tr>
<tr>
<td>
<span id="line-422" class="anchor"></span><p class="line891"><tt>wx.LIST_STATE_DONTCARE</tt></p>
</td>
<td><p class="line862">无关状态。这个状态只在微软<tt>Windows</tt>下有效。</p></td>
</tr>
<tr>
<td>
<span id="line-423" class="anchor"></span><p class="line891"><tt>wx.LIST_STATE_DropHILITED</tt></p>
</td>
<td><p class="line862">拖放状态。项目显示为高亮，这个状态只在微软<tt>Windows</tt>下有效。</p></td>
</tr>
<tr>
<td>
<span id="line-424" class="anchor"></span><p class="line891"><tt>wx.LIST_STATE_FOCUSED</tt></p>
</td>
<td><p class="line862">获得光标焦点状态。</p></td>
</tr>
<tr>
<td>
<span id="line-425" class="anchor"></span><p class="line891"><tt>wx.LIST_STATE_SelectED</tt></p>
</td>
<td><p class="line862">被选中状态。</p></td>
</tr>
</tbody></table></div>
<span id="line-426" class="anchor"></span><span id="line-427" class="anchor"></span><p class="line862">你也可以用<tt>SetColumn(col,
item)</tt>方法对一个已添加的列进行设置。你也可以在程序中用<tt>GetColumnWidth(col)</tt>方法方法得到一个列的宽度，该方法返回列表的宽度（像素单位）——显然这只对报告模式的列表有用。你可以使用<tt>SetColumnWidth(col,width)</tt>来设置列的宽度。这个<tt>width</tt>可以是一个整数值或特殊值，这些特殊值有：<tt>wx.LIST_AUTOSIZE</tt>，它将列的宽度设置为最长项目的宽度，或<tt>wx.LIST_AUTOSIZE_USEHEADER</tt>，它将宽度设置为列的首部文本（列标题）的宽度。在非<tt>Windows</tt>操作系统下，<tt>wx.LIST_AUTOSIZE_USEHEADER</tt>可能只自动地将列宽度设置到80像素。
<span id="line-428" class="anchor"></span><span id="line-429" class="anchor"></span></p>
<p class="line862">如果你对已有的索引不清楚了，你可以查询列表中项目的数量。方法有<tt>GetColumnCount()</tt>，它返回列表中所定义的列的数量，<tt>GetItemCount()</tt>返回行的数量。如果你的列表是列表模式，那么方法<tt>GetCountPerPage()</tt>返回每列中项目的数量。 <span id="line-430" class="anchor"></span><span id="line-431" class="anchor"></span></p>
<p class="line862">要从列表中删除项目，使用<tt>DeleteItem(item)</tt>方法，参数<tt>item</tt>是项目在列表中的索引。如果你想一次删除所有的项目，可以使用<tt>DeleteAllItems()</tt>或<tt>ClearAll()</tt>。你可以使用<tt>DeleteColumn(col)</tt>删除一列，<tt>col</tt>是列的索引。 <span id="line-432" class="anchor"></span><span id="line-433" class="anchor"></span><span id="line-434" class="anchor"></span><span id="line-435" class="anchor"></span><span id="line-436" class="anchor"></span></p>
<p class="line867">
</p>
<h2 id="head-2af1e7d9285f5a57d542aa7fc512513fb30cbc16">3. 响应用户</h2>
<span id="line-437" class="anchor"></span><span id="line-438" class="anchor"></span><span id="line-439" class="anchor"></span><p class="line874">通常，一个列表控件在当用户选择了列表中的一个项目后都要做一些事情。在接下来的部分， 我们将展示一个列表控件都能响应哪些事件，并提供一个使用列表控件事件的例子。 <span id="line-440" class="anchor"></span><span id="line-441" class="anchor"></span><span id="line-442" class="anchor"></span></p>
<p class="line867">
</p>
<h3 id="head-019bea11562b9101eb4374af871f0d740b7d4b35">3.1. 如何响应用户在列表中的选择？</h3>
<span id="line-443" class="anchor"></span><span id="line-444" class="anchor"></span><span id="line-445" class="anchor"></span><p class="line862">像别的控件一样，列表控件也触发事件以响应用户的动作。你可以像我们在第三章那样使用<tt>Bind()</tt>方法为这些 <span id="line-446" class="anchor"></span>事件设置处理器。所有这些事件处理器都接受一个<tt>wx.ListEvent</tt>实例，<tt>wx.ListEvent</tt>是<tt>wx.CommandEvent</tt>的子类。<tt>wx.ListEvent</tt>有少量专用的<tt>get</tt>*方法。某些属性只对特定的事件类型有效，这些特定的事件类型在本章的另外部分说明。适用于所有事件类型的属性见表13.4。 <span id="line-447" class="anchor"></span><span id="line-448" class="anchor"></span><span id="line-449" class="anchor"></span></p>
<p class="line867"><strong>表13.4</strong>  <span id="line-450" class="anchor"></span><strong><tt>wx.ListEvent</tt>的属性</strong> <span id="line-451" class="anchor"></span><span id="line-452" class="anchor"></span></p>
<div><table><tbody>
<tr>
<td><p class="line891"><tt>GetData()</tt></p></td>
<td><p class="line862">与该事件的列表项相关的用户数据项</p></td>
</tr>
<tr>
<td>
<span id="line-453" class="anchor"></span><p class="line891"><tt>GetKeyCode()</tt></p>
</td>
<td><p class="line862">在一个按键事件中，所按下的键的键码</p></td>
</tr>
<tr>
<td>
<span id="line-454" class="anchor"></span><p class="line891"><tt>GetIndex()</tt></p>
</td>
<td><p class="line862">得到列表中与事件相关的项目的索引</p></td>
</tr>
<tr>
<td>
<span id="line-455" class="anchor"></span><p class="line891"><tt>GetItem()</tt></p>
</td>
<td><p class="line862">得到与事件相关的实际的<tt>wx.ListItem</tt></p></td>
</tr>
<tr>
<td>
<span id="line-456" class="anchor"></span><p class="line891"><tt>GetImage()</tt></p>
</td>
<td><p class="line862">得到与事件相关单元格中的图像</p></td>
</tr>
<tr>
<td>
<span id="line-457" class="anchor"></span><p class="line891"><tt>GetMask()</tt></p>
</td>
<td><p class="line862">得到与事件相关单元格中的位掩码</p></td>
</tr>
<tr>
<td>
<span id="line-458" class="anchor"></span><p class="line891"><tt>GetPoint()</tt></p>
</td>
<td><p class="line862">产生事件的实际的鼠标位置</p></td>
</tr>
<tr>
<td>
<span id="line-459" class="anchor"></span><p class="line891"><tt>GetText()</tt></p>
</td>
<td><p class="line862">得到与事件相关的单元格中的文本</p></td>
</tr>
</tbody></table></div>
<span id="line-460" class="anchor"></span><span id="line-461" class="anchor"></span><p class="line862">这儿有几个关于<tt>wx.ListEvent</tt>的不同的事件类型，每个都可以有一个不同的处理器。某些关联性更强的事件将在后面的部分讨论。表13.5列出了选择列表中的项目时的所有事件类型。 <span id="line-462" class="anchor"></span><span id="line-463" class="anchor"></span><span id="line-464" class="anchor"></span></p>
<p class="line867"><strong>表13.5</strong>  <span id="line-465" class="anchor"></span><strong>与选择一个列表控件中的项目相关的事件类型</strong> <span id="line-466" class="anchor"></span><span id="line-467" class="anchor"></span></p>
<div><table><tbody>
<tr>
<td><p class="line891"><tt>EVT_LIST_BEGIN_DRAG</tt></p></td>
<td><p class="line862">当用户使用鼠标左按键开始一个拖动操作时，触发该事件</p></td>
</tr>
<tr>
<td>
<span id="line-468" class="anchor"></span><p class="line891"><tt>EVT_LIST_BEGIN_RDRAG</tt></p>
</td>
<td><p class="line862">当用户使用鼠标右按键开始一个拖动操作时，触发该事件</p></td>
</tr>
<tr>
<td>
<span id="line-469" class="anchor"></span><p class="line891"><tt>EVT_LIST_Delete_ALL_ITEMS</tt></p>
</td>
<td><p class="line862">调用列表的 <tt>DeleteAll()</tt>将触发该事件</p></td>
</tr>
<tr>
<td>
<span id="line-470" class="anchor"></span><p class="line891"><tt>EVT_LIST_Delete_ITEM</tt></p>
</td>
<td><p class="line862">调用列表的 <tt>Delete()</tt>将触发该事件</p></td>
</tr>
<tr>
<td>
<span id="line-471" class="anchor"></span><p class="line891"><tt>EVT_LIST_Insert_ITEM</tt></p>
</td>
<td><p class="line862">当一个项目被插入到列表中时，触发该事件</p></td>
</tr>
<tr>
<td>
<span id="line-472" class="anchor"></span><p class="line891"><tt>EVT_LIST_ITEM_ACTIVATED</tt></p>
</td>
<td><p class="line862">用户通过在已选择的项目上按下回车或双击来激活一个项目时</p></td>
</tr>
<tr>
<td>
<span id="line-473" class="anchor"></span><p class="line891"><tt>EVT_LIST_ITEM_DESelectED</tt></p>
</td>
<td><p class="line862">当项目被取消选择时触发该事件</p></td>
</tr>
<tr>
<td>
<span id="line-474" class="anchor"></span><p class="line891"><tt>EVT_LIST_ITEM_FOCUSED</tt></p>
</td>
<td><p class="line862">当项目的焦点变化时触发该事件</p></td>
</tr>
<tr>
<td>
<span id="line-475" class="anchor"></span><p class="line891"><tt>EVT_LIST_ITEM_MIDDLE_CLICK</tt></p>
</td>
<td><p class="line862">当在列表上敲击了鼠标的中间按钮时触发该事件</p></td>
</tr>
<tr>
<td>
<span id="line-476" class="anchor"></span><p class="line891"><tt>EVT_LIST_ITEM_RIGHT_CLICK</tt></p>
</td>
<td><p class="line862">当在列表上敲击了鼠标的右按钮时触发该事件</p></td>
</tr>
<tr>
<td>
<span id="line-477" class="anchor"></span><p class="line891"><tt>EVT_LIST_ITEM_SelectED</tt></p>
</td>
<td><p class="line862">当通过敲击鼠标左按钮来选择一个项目时，触发该事件</p></td>
</tr>
<tr>
<td>
<span id="line-478" class="anchor"></span><p class="line891"><tt>EVT_LIST_ITEM_KEY_DOWN</tt></p>
</td>
<td><p class="line862">在列表控件已经获得了焦点时，一个按键被按下将触发该事件</p></td>
</tr>
</tbody></table></div>
<span id="line-479" class="anchor"></span><span id="line-480" class="anchor"></span><p class="line874">下节中例13.3将提供一个关于上述事件中的一些事件的应用例子。 <span id="line-481" class="anchor"></span><span id="line-482" class="anchor"></span><span id="line-483" class="anchor"></span></p>
<p class="line867">
</p>
<h3 id="head-c27fea646e237f8bd65676229bdef0a7770cdcd0">3.2. 如何响应用户在一个列的首部中的选择？</h3>
<span id="line-484" class="anchor"></span><span id="line-485" class="anchor"></span><span id="line-486" class="anchor"></span><p class="line862">除了用户在列表体中触发的事件以外，还有在报告列表控件的列首中所触发的事件。列事件创建的<tt>wx.ListEvent</tt>对象有另一个方法：<tt>GetColumn()</tt>，该方法返回产生事件的列的索引。如果事件是一个列边框的拖动事件，那么这个索引是所拖动的边框的左边位置。如果事件是一个敲击所触发的，且敲击不在列内，那么该方法返回－1。表13.6包含了列事件类型的列表。 <span id="line-487" class="anchor"></span><span id="line-488" class="anchor"></span><span id="line-489" class="anchor"></span></p>
<p class="line867"><strong>表13.6</strong>  <span id="line-490" class="anchor"></span><strong>列表控件列事件类型</strong> <span id="line-491" class="anchor"></span><span id="line-492" class="anchor"></span></p>
<div><table><tbody>
<tr>
<td><p class="line891"><tt>EVT_LIST_COL_BEGIN_DRAG</tt></p></td>
<td><p class="line862">当用户开始拖动一个列的边框时，触发该事件</p></td>
</tr>
<tr>
<td>
<span id="line-493" class="anchor"></span><p class="line891"><tt>EVT_LIST_COL_CLICK</tt></p>
</td>
<td><p class="line862">列表首部内的一个敲击将触发该事件</p></td>
</tr>
<tr>
<td>
<span id="line-494" class="anchor"></span><p class="line891"><tt>EVT_LIST_COL_RIGHT_CLICK</tt></p>
</td>
<td><p class="line862">列表首部内的一个右击将触发该事件</p></td>
</tr>
<tr>
<td>
<span id="line-495" class="anchor"></span><p class="line891"><tt>EVT_LiST_COL_END_DRAG</tt></p>
</td>
<td><p class="line862">当用户完成对一个列表边框的拖动时，触发该事件</p></td>
</tr>
</tbody></table></div>
<span id="line-496" class="anchor"></span><span id="line-497" class="anchor"></span><p class="line874">例13.3显示了一些列表事件的处理，并也提供了方法的一些演示。 <span id="line-498" class="anchor"></span><span id="line-499" class="anchor"></span><span id="line-500" class="anchor"></span></p>
<p class="line867"><strong>例13.3</strong>  <span id="line-501" class="anchor"></span><strong>一些不同列表事件和属性的一个例子</strong> <span id="line-502" class="anchor"></span><span id="line-503" class="anchor"></span><span id="line-504" class="anchor"></span><span id="line-505" class="anchor"></span><span id="line-506" class="anchor"></span><span id="line-507" class="anchor"></span><span id="line-508" class="anchor"></span><span id="line-509" class="anchor"></span><span id="line-510" class="anchor"></span><span id="line-511" class="anchor"></span><span id="line-512" class="anchor"></span><span id="line-513" class="anchor"></span><span id="line-514" class="anchor"></span><span id="line-515" class="anchor"></span><span id="line-516" class="anchor"></span><span id="line-517" class="anchor"></span><span id="line-518" class="anchor"></span><span id="line-519" class="anchor"></span><span id="line-520" class="anchor"></span><span id="line-521" class="anchor"></span><span id="line-522" class="anchor"></span><span id="line-523" class="anchor"></span><span id="line-524" class="anchor"></span><span id="line-525" class="anchor"></span><span id="line-526" class="anchor"></span><span id="line-527" class="anchor"></span><span id="line-528" class="anchor"></span><span id="line-529" class="anchor"></span><span id="line-530" class="anchor"></span><span id="line-531" class="anchor"></span><span id="line-532" class="anchor"></span><span id="line-533" class="anchor"></span><span id="line-534" class="anchor"></span><span id="line-535" class="anchor"></span><span id="line-536" class="anchor"></span><span id="line-537" class="anchor"></span><span id="line-538" class="anchor"></span><span id="line-539" class="anchor"></span><span id="line-540" class="anchor"></span><span id="line-541" class="anchor"></span><span id="line-542" class="anchor"></span><span id="line-543" class="anchor"></span><span id="line-544" class="anchor"></span><span id="line-545" class="anchor"></span><span id="line-546" class="anchor"></span><span id="line-547" class="anchor"></span><span id="line-548" class="anchor"></span><span id="line-549" class="anchor"></span><span id="line-550" class="anchor"></span><span id="line-551" class="anchor"></span><span id="line-552" class="anchor"></span><span id="line-553" class="anchor"></span><span id="line-554" class="anchor"></span><span id="line-555" class="anchor"></span><span id="line-556" class="anchor"></span><span id="line-557" class="anchor"></span><span id="line-558" class="anchor"></span><span id="line-559" class="anchor"></span><span id="line-560" class="anchor"></span><span id="line-561" class="anchor"></span><span id="line-562" class="anchor"></span><span id="line-563" class="anchor"></span><span id="line-564" class="anchor"></span><span id="line-565" class="anchor"></span><span id="line-566" class="anchor"></span><span id="line-567" class="anchor"></span><span id="line-568" class="anchor"></span><span id="line-569" class="anchor"></span><span id="line-570" class="anchor"></span><span id="line-571" class="anchor"></span><span id="line-572" class="anchor"></span><span id="line-573" class="anchor"></span><span id="line-574" class="anchor"></span><span id="line-575" class="anchor"></span><span id="line-576" class="anchor"></span><span id="line-577" class="anchor"></span><span id="line-578" class="anchor"></span><span id="line-579" class="anchor"></span><span id="line-580" class="anchor"></span><span id="line-581" class="anchor"></span><span id="line-582" class="anchor"></span><span id="line-583" class="anchor"></span><span id="line-584" class="anchor"></span><span id="line-585" class="anchor"></span><span id="line-586" class="anchor"></span><span id="line-587" class="anchor"></span><span id="line-588" class="anchor"></span><span id="line-589" class="anchor"></span><span id="line-590" class="anchor"></span><span id="line-591" class="anchor"></span><span id="line-592" class="anchor"></span><span id="line-593" class="anchor"></span><span id="line-594" class="anchor"></span><span id="line-595" class="anchor"></span><span id="line-596" class="anchor"></span><span id="line-597" class="anchor"></span><span id="line-598" class="anchor"></span><span id="line-599" class="anchor"></span><span id="line-600" class="anchor"></span><span id="line-601" class="anchor"></span><span id="line-602" class="anchor"></span><span id="line-603" class="anchor"></span><span id="line-604" class="anchor"></span><span id="line-605" class="anchor"></span><span id="line-606" class="anchor"></span><span id="line-607" class="anchor"></span><span id="line-608" class="anchor"></span><span id="line-609" class="anchor"></span><span id="line-610" class="anchor"></span><span id="line-611" class="anchor"></span><span id="line-612" class="anchor"></span><span id="line-613" class="anchor"></span><span id="line-614" class="anchor"></span><span id="line-615" class="anchor"></span><span id="line-616" class="anchor"></span><span id="line-617" class="anchor"></span><span id="line-618" class="anchor"></span><span id="line-619" class="anchor"></span><span id="line-620" class="anchor"></span><span id="line-621" class="anchor"></span><span id="line-622" class="anchor"></span><span id="line-623" class="anchor"></span><span id="line-624" class="anchor"></span><span id="line-625" class="anchor"></span><span id="line-626" class="anchor"></span><span id="line-627" class="anchor"></span><span id="line-628" class="anchor"></span><span id="line-629" class="anchor"></span><span id="line-630" class="anchor"></span><span id="line-631" class="anchor"></span><span id="line-632" class="anchor"></span><span id="line-633" class="anchor"></span><span id="line-634" class="anchor"></span><span id="line-635" class="anchor"></span><span id="line-636" class="anchor"></span><span id="line-637" class="anchor"></span><span id="line-638" class="anchor"></span><span id="line-639" class="anchor"></span><span id="line-640" class="anchor"></span><span id="line-641" class="anchor"></span><span id="line-642" class="anchor"></span><span id="line-643" class="anchor"></span><span id="line-644" class="anchor"></span><span id="line-645" class="anchor"></span><span id="line-646" class="anchor"></span><span id="line-647" class="anchor"></span><span id="line-648" class="anchor"></span><span id="line-649" class="anchor"></span><span id="line-650" class="anchor"></span><span id="line-651" class="anchor"></span><span id="line-652" class="anchor"></span><span id="line-653" class="anchor"></span><span id="line-654" class="anchor"></span><span id="line-655" class="anchor"></span><span id="line-656" class="anchor"></span><span id="line-657" class="anchor"></span><span id="line-658" class="anchor"></span><span id="line-659" class="anchor"></span><span id="line-660" class="anchor"></span><span id="line-661" class="anchor"></span><span id="line-662" class="anchor"></span><span id="line-663" class="anchor"></span><span id="line-664" class="anchor"></span><span id="line-665" class="anchor"></span><span id="line-666" class="anchor"></span><span id="line-667" class="anchor"></span><span id="line-668" class="anchor"></span><span id="line-669" class="anchor"></span><span id="line-670" class="anchor"></span><span id="line-671" class="anchor"></span><span id="line-672" class="anchor"></span><span id="line-673" class="anchor"></span><span id="line-674" class="anchor"></span><span id="line-675" class="anchor"></span><span id="line-676" class="anchor"></span><span id="line-677" class="anchor"></span><span id="line-678" class="anchor"></span><span id="line-679" class="anchor"></span><span id="line-680" class="anchor"></span><span id="line-681" class="anchor"></span><span id="line-682" class="anchor"></span><span id="line-683" class="anchor"></span><span id="line-684" class="anchor"></span><span id="line-685" class="anchor"></span><span id="line-686" class="anchor"></span><span id="line-687" class="anchor"></span><span id="line-688" class="anchor"></span><span id="line-689" class="anchor"></span><span id="line-690" class="anchor"></span><span id="line-691" class="anchor"></span><span id="line-692" class="anchor"></span><span id="line-693" class="anchor"></span><span id="line-694" class="anchor"></span><span id="line-695" class="anchor"></span><span id="line-696" class="anchor"></span><span id="line-697" class="anchor"></span><span id="line-698" class="anchor"></span><span id="line-699" class="anchor"></span><span id="line-700" class="anchor"></span><span id="line-701" class="anchor"></span><span id="line-702" class="anchor"></span><span id="line-703" class="anchor"></span><span id="line-704" class="anchor"></span><span id="line-705" class="anchor"></span><span id="line-706" class="anchor"></span><span id="line-707" class="anchor"></span><span id="line-708" class="anchor"></span><span id="line-709" class="anchor"></span><span id="line-710" class="anchor"></span><span id="line-711" class="anchor"></span><span id="line-712" class="anchor"></span><span id="line-713" class="anchor"></span></p>
<div lang="en" dir="ltr" class="codearea">
<script type="text/javascript">
document.write('<a href="#" onclick="return togglenumber(\'CA-d666312b6074fde64687c1645f9b32029c36e80e_006\', 1, 1);" \
class="codenumbers">切换行号显示<\/a>');
</script><a class="codenumbers" onclick="return togglenumber('CA-d666312b6074fde64687c1645f9b32029c36e80e_006', 1, 1);" href="#">切换行号显示</a>
<pre lang="en" id="CA-d666312b6074fde64687c1645f9b32029c36e80e_006" dir="ltr"><span class="line"><span class="LineNumber">   1 </span><span class="ResWord">import</span> <span class="ID">wx</span></span>
<span class="line"><span class="LineNumber">   2 </span><span class="ResWord">import</span> <span class="ID">sys</span><span class="Operator">,</span> <span class="ID">glob</span><span class="Operator">,</span> <span class="ID">random</span></span>
<span class="line"><span class="LineNumber">   3 </span><span class="ResWord">import</span> <span class="ID">data</span></span>
<span class="line"><span class="LineNumber">   4 </span></span>
<span class="line"><span class="LineNumber">   5 </span><span class="ResWord">class</span> <span class="ID">DemoFrame</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">Frame</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">   6 </span>    <span class="ResWord">def</span> <span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">   7 </span>        <span class="ID">wx</span><span class="Operator">.</span><span class="ID">Frame</span><span class="Operator">.</span><span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">None</span><span class="Operator">,</span> <span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">   8 </span>                          <span class="String">"Other wx.ListCtrl Stuff"</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">   9 </span>                          <span class="ID">size</span><span class="Operator">=</span><span class="Operator">(</span><span class="Number">700</span><span class="Operator">,</span><span class="Number">500</span><span class="Operator">)</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  10 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span> <span class="Operator">=</span> <span class="ID">None</span></span>
<span class="line"><span class="LineNumber">  11 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">editable</span> <span class="Operator">=</span> <span class="ID">False</span></span>
<span class="line"><span class="LineNumber">  12 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">MakeMenu</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  13 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">MakeListCtrl</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  14 </span></span>
<span class="line"><span class="LineNumber">  15 </span></span>
<span class="line"><span class="LineNumber">  16 </span>    <span class="ResWord">def</span> <span class="ID">MakeListCtrl</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">otherflags</span><span class="Operator">=</span><span class="Number">0</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  17 </span>        <span class="Comment"># if we already have a listctrl then get rid of it</span></span>
<span class="line"><span class="LineNumber">  18 </span><span class="Comment"></span>        <span class="ResWord">if</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  19 </span>            <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">Destroy</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  20 </span></span>
<span class="line"><span class="LineNumber">  21 </span>        <span class="ResWord">if</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">editable</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  22 </span>            <span class="ID">otherflags</span> <span class="Operator">|=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">LC_EDIT_LABELS</span></span>
<span class="line"><span class="LineNumber">  23 </span></span>
<span class="line"><span class="LineNumber">  24 </span>        <span class="Comment"># load some images into an image list</span></span>
<span class="line"><span class="LineNumber">  25 </span><span class="Comment"></span>        <span class="ID">il</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">ImageList</span><span class="Operator">(</span><span class="Number">16</span><span class="Operator">,</span><span class="Number">16</span><span class="Operator">,</span> <span class="ID">True</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  26 </span>        <span class="ResWord">for</span> <span class="ID">name</span> <span class="ResWord">in</span> <span class="ID">glob</span><span class="Operator">.</span><span class="ID">glob</span><span class="Operator">(</span><span class="String">"smicon??.png"</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  27 </span>            <span class="ID">bmp</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">Bitmap</span><span class="Operator">(</span><span class="ID">name</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">BITMAP_TYPE_PNG</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  28 </span>            <span class="ID">il_max</span> <span class="Operator">=</span> <span class="ID">il</span><span class="Operator">.</span><span class="ID">Add</span><span class="Operator">(</span><span class="ID">bmp</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  29 </span></span>
<span class="line"><span class="LineNumber">  30 </span>        <span class="Comment"># create the list control</span></span>
<span class="line"><span class="LineNumber">  31 </span><span class="Comment"></span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">ListCtrl</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span> <span class="ID">style</span><span class="Operator">=</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">LC_REPORT</span><span class="Operator">|</span><span class="ID">otherflags</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  32 </span></span>
<span class="line"><span class="LineNumber">  33 </span>        <span class="Comment"># assign the image list to it</span></span>
<span class="line"><span class="LineNumber">  34 </span><span class="Comment"></span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">AssignImageList</span><span class="Operator">(</span><span class="ID">il</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">IMAGE_LIST_SMALL</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  35 </span></span>
<span class="line"><span class="LineNumber">  36 </span>        <span class="Comment"># Add some columns</span></span>
<span class="line"><span class="LineNumber">  37 </span><span class="Comment"></span>        <span class="ResWord">for</span> <span class="ID">col</span><span class="Operator">,</span> <span class="ID">text</span> <span class="ResWord">in</span> <span class="ID">enumerate</span><span class="Operator">(</span><span class="ID">data</span><span class="Operator">.</span><span class="ID">columns</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  38 </span>            <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">InsertColumn</span><span class="Operator">(</span><span class="ID">col</span><span class="Operator">,</span> <span class="ID">text</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  39 </span></span>
<span class="line"><span class="LineNumber">  40 </span>        <span class="Comment"># add the rows</span></span>
<span class="line"><span class="LineNumber">  41 </span><span class="Comment"></span>        <span class="ResWord">for</span> <span class="ID">row</span><span class="Operator">,</span> <span class="ID">item</span> <span class="ResWord">in</span> <span class="ID">enumerate</span><span class="Operator">(</span><span class="ID">data</span><span class="Operator">.</span><span class="ID">rows</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  42 </span>            <span class="ID">index</span> <span class="Operator">=</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">InsertStringItem</span><span class="Operator">(</span><span class="ID">sys</span><span class="Operator">.</span><span class="ID">maxint</span><span class="Operator">,</span>
<span class="ID">item</span><span class="Operator">[</span><span class="Number">0</span><span class="Operator">]</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  43 </span>            <span class="ResWord">for</span> <span class="ID">col</span><span class="Operator">,</span> <span class="ID">text</span> <span class="ResWord">in</span> <span class="ID">enumerate</span><span class="Operator">(</span><span class="ID">item</span><span class="Operator">[</span><span class="Number">1</span><span class="Operator">:</span><span class="Operator">]</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  44 </span>                <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">SetStringItem</span><span class="Operator">(</span><span class="ID">index</span><span class="Operator">,</span> <span class="ID">col</span><span class="Operator">+</span><span class="Number">1</span><span class="Operator">,</span>
<span class="ID">text</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  45 </span></span>
<span class="line"><span class="LineNumber">  46 </span>            <span class="Comment"># give each item a random image</span></span>
<span class="line"><span class="LineNumber">  47 </span><span class="Comment"></span>            <span class="ID">img</span> <span class="Operator">=</span> <span class="ID">random</span><span class="Operator">.</span><span class="ID">randint</span><span class="Operator">(</span><span class="Number">0</span><span class="Operator">,</span> <span class="ID">il_max</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  48 </span>            <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">SetItemImage</span><span class="Operator">(</span><span class="ID">index</span><span class="Operator">,</span> <span class="ID">img</span><span class="Operator">,</span> <span class="ID">img</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  49 </span></span>
<span class="line"><span class="LineNumber">  50 </span>            <span class="Comment"># set the data value for each item to be its position in</span></span>
<span class="line"><span class="LineNumber">  51 </span><span class="Comment"></span>            <span class="Comment"># the data list</span></span>
<span class="line"><span class="LineNumber">  52 </span><span class="Comment"></span>            <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">SetItemData</span><span class="Operator">(</span><span class="ID">index</span><span class="Operator">,</span> <span class="ID">row</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  53 </span></span>
<span class="line"><span class="LineNumber">  54 </span></span>
<span class="line"><span class="LineNumber">  55 </span>        <span class="Comment"># set the width of the columns in various ways</span></span>
<span class="line"><span class="LineNumber">  56 </span><span class="Comment"></span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">SetColumnWidth</span><span class="Operator">(</span><span class="Number">0</span><span class="Operator">,</span> <span class="Number">120</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  57 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">SetColumnWidth</span><span class="Operator">(</span><span class="Number">1</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">LIST_AUTOSIZE</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  58 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">SetColumnWidth</span><span class="Operator">(</span><span class="Number">2</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">LIST_AUTOSIZE</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  59 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">SetColumnWidth</span><span class="Operator">(</span><span class="Number">3</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">LIST_AUTOSIZE_USEHEADER</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  60 </span></span>
<span class="line"><span class="LineNumber">  61 </span>        <span class="Comment"># bind some interesting events</span></span>
<span class="line"><span class="LineNumber">  62 </span><span class="Comment"></span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">Bind</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">EVT_LIST_ITEM_SelectED</span><span class="Operator">,</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">OnItemSelected</span><span class="Operator">,</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  63 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">Bind</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">EVT_LIST_ITEM_DESelectED</span><span class="Operator">,</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">OnItemDeselected</span><span class="Operator">,</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  64 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">Bind</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">EVT_LIST_ITEM_ACTIVATED</span><span class="Operator">,</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">OnItemActivated</span><span class="Operator">,</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  65 </span></span>
<span class="line"><span class="LineNumber">  66 </span>        <span class="Comment"># in case we are recreating the list tickle the frame a bit so</span></span>
<span class="line"><span class="LineNumber">  67 </span><span class="Comment"></span>        <span class="Comment"># it will redo the layout</span></span>
<span class="line"><span class="LineNumber">  68 </span><span class="Comment"></span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">SendSizeEvent</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  69 </span></span>
<span class="line"><span class="LineNumber">  70 </span></span>
<span class="line"><span class="LineNumber">  71 </span>    <span class="ResWord">def</span> <span class="ID">MakeMenu</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  72 </span>        <span class="ID">mbar</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">MenuBar</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  73 </span>        <span class="ID">menu</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">Menu</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  74 </span>        <span class="ID">item</span> <span class="Operator">=</span> <span class="ID">menu</span><span class="Operator">.</span><span class="ID">Append</span><span class="Operator">(</span><span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span> <span class="String">"E \tAlt-X"</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  75 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">Bind</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">EVT_MENU</span><span class="Operator">,</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">OnExit</span><span class="Operator">,</span> <span class="ID">item</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  76 </span>        <span class="ID">mbar</span><span class="Operator">.</span><span class="ID">Append</span><span class="Operator">(</span><span class="ID">menu</span><span class="Operator">,</span> <span class="String">" "</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  77 </span></span>
<span class="line"><span class="LineNumber">  78 </span>        <span class="ID">menu</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">Menu</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  79 </span>        <span class="ID">item</span> <span class="Operator">=</span> <span class="ID">menu</span><span class="Operator">.</span><span class="ID">Append</span><span class="Operator">(</span><span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span> <span class="String">"Sort ascending"</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  80 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">Bind</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">EVT_MENU</span><span class="Operator">,</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">OnSortAscending</span><span class="Operator">,</span> <span class="ID">item</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  81 </span>        <span class="ID">item</span> <span class="Operator">=</span> <span class="ID">menu</span><span class="Operator">.</span><span class="ID">Append</span><span class="Operator">(</span><span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span> <span class="String">"Sort descending"</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  82 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">Bind</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">EVT_MENU</span><span class="Operator">,</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">OnSortDescending</span><span class="Operator">,</span> <span class="ID">item</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  83 </span>        <span class="ID">item</span> <span class="Operator">=</span> <span class="ID">menu</span><span class="Operator">.</span><span class="ID">Append</span><span class="Operator">(</span><span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span> <span class="String">"Sort by submitter"</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  84 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">Bind</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">EVT_MENU</span><span class="Operator">,</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">OnSortBySubmitter</span><span class="Operator">,</span> <span class="ID">item</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  85 </span></span>
<span class="line"><span class="LineNumber">  86 </span>        <span class="ID">menu</span><span class="Operator">.</span><span class="ID">AppendSeparator</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  87 </span>        <span class="ID">item</span> <span class="Operator">=</span> <span class="ID">menu</span><span class="Operator">.</span><span class="ID">Append</span><span class="Operator">(</span><span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span> <span class="String">"Show selected"</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  88 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">Bind</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">EVT_MENU</span><span class="Operator">,</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">OnShowSelected</span><span class="Operator">,</span> <span class="ID">item</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  89 </span>        <span class="ID">item</span> <span class="Operator">=</span> <span class="ID">menu</span><span class="Operator">.</span><span class="ID">Append</span><span class="Operator">(</span><span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span> <span class="String">"Select all"</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  90 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">Bind</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">EVT_MENU</span><span class="Operator">,</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">OnSelectAll</span><span class="Operator">,</span> <span class="ID">item</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  91 </span>        <span class="ID">item</span> <span class="Operator">=</span> <span class="ID">menu</span><span class="Operator">.</span><span class="ID">Append</span><span class="Operator">(</span><span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span> <span class="String">"Select none"</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  92 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">Bind</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">EVT_MENU</span><span class="Operator">,</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">OnSelectNone</span><span class="Operator">,</span> <span class="ID">item</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  93 </span></span>
<span class="line"><span class="LineNumber">  94 </span>        <span class="ID">menu</span><span class="Operator">.</span><span class="ID">AppendSeparator</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  95 </span>        <span class="ID">item</span> <span class="Operator">=</span> <span class="ID">menu</span><span class="Operator">.</span><span class="ID">Append</span><span class="Operator">(</span><span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span> <span class="String">"Set item text colour"</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  96 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">Bind</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">EVT_MENU</span><span class="Operator">,</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">OnSetTextColour</span><span class="Operator">,</span> <span class="ID">item</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  97 </span>        <span class="ID">item</span> <span class="Operator">=</span> <span class="ID">menu</span><span class="Operator">.</span><span class="ID">Append</span><span class="Operator">(</span><span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span> <span class="String">"Set item background colour"</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  98 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">Bind</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">EVT_MENU</span><span class="Operator">,</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">OnSetBGColour</span><span class="Operator">,</span> <span class="ID">item</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  99 </span></span>
<span class="line"><span class="LineNumber"> 100 </span>        <span class="ID">menu</span><span class="Operator">.</span><span class="ID">AppendSeparator</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber"> 101 </span>        <span class="ID">item</span> <span class="Operator">=</span> <span class="ID">menu</span><span class="Operator">.</span><span class="ID">Append</span><span class="Operator">(</span><span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span> <span class="String">"Enable item editing"</span><span class="Operator">,</span> <span class="ID">kind</span><span class="Operator">=</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">ITEM_CHECK</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber"> 102 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">Bind</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">EVT_MENU</span><span class="Operator">,</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">OnEnableEditing</span><span class="Operator">,</span> <span class="ID">item</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber"> 103 </span>        <span class="ID">item</span> <span class="Operator">=</span> <span class="ID">menu</span><span class="Operator">.</span><span class="ID">Append</span><span class="Operator">(</span><span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span> <span class="String">"Edit current item"</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber"> 104 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">Bind</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">EVT_MENU</span><span class="Operator">,</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">OnEditItem</span><span class="Operator">,</span> <span class="ID">item</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber"> 105 </span>        <span class="ID">mbar</span><span class="Operator">.</span><span class="ID">Append</span><span class="Operator">(</span><span class="ID">menu</span><span class="Operator">,</span> <span class="String">" "</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber"> 106 </span></span>
<span class="line"><span class="LineNumber"> 107 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">SetMenuBar</span><span class="Operator">(</span><span class="ID">mbar</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber"> 108 </span></span>
<span class="line"><span class="LineNumber"> 109 </span></span>
<span class="line"><span class="LineNumber"> 110 </span></span>
<span class="line"><span class="LineNumber"> 111 </span>    <span class="ResWord">def</span> <span class="ID">OnExit</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">evt</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber"> 112 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">Close</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber"> 113 </span></span>
<span class="line"><span class="LineNumber"> 114 </span></span>
<span class="line"><span class="LineNumber"> 115 </span>    <span class="ResWord">def</span> <span class="ID">OnItemSelected</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">evt</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber"> 116 </span>        <span class="ID">item</span> <span class="Operator">=</span> <span class="ID">evt</span><span class="Operator">.</span><span class="ID">GetItem</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber"> 117 </span>        <span class="ResWord">print</span> <span class="String">"Item selected:"</span><span class="Operator">,</span> <span class="ID">item</span><span class="Operator">.</span><span class="ID">GetText</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber"> 118 </span></span>
<span class="line"><span class="LineNumber"> 119 </span>    <span class="ResWord">def</span> <span class="ID">OnItemDeselected</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">evt</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber"> 120 </span>        <span class="ID">item</span> <span class="Operator">=</span> <span class="ID">evt</span><span class="Operator">.</span><span class="ID">GetItem</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber"> 121 </span>        <span class="ResWord">print</span> <span class="String">"Item deselected:"</span><span class="Operator">,</span> <span class="ID">item</span><span class="Operator">.</span><span class="ID">GetText</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber"> 122 </span></span>
<span class="line"><span class="LineNumber"> 123 </span>    <span class="ResWord">def</span> <span class="ID">OnItemActivated</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">evt</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber"> 124 </span>        <span class="ID">item</span> <span class="Operator">=</span> <span class="ID">evt</span><span class="Operator">.</span><span class="ID">GetItem</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber"> 125 </span>        <span class="ResWord">print</span> <span class="String">"Item activated:"</span><span class="Operator">,</span> <span class="ID">item</span><span class="Operator">.</span><span class="ID">GetText</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber"> 126 </span></span>
<span class="line"><span class="LineNumber"> 127 </span>    <span class="ResWord">def</span> <span class="ID">OnSortAscending</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">evt</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber"> 128 </span>        <span class="Comment"># recreate the listctrl with a sort style</span></span>
<span class="line"><span class="LineNumber"> 129 </span><span class="Comment"></span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">MakeListCtrl</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">LC_SORT_ASCENDING</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber"> 130 </span></span>
<span class="line"><span class="LineNumber"> 131 </span>    <span class="ResWord">def</span> <span class="ID">OnSortDescending</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">evt</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber"> 132 </span>        <span class="Comment"># recreate the listctrl with a sort style</span></span>
<span class="line"><span class="LineNumber"> 133 </span><span class="Comment"></span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">MakeListCtrl</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">LC_SORT_DESCENDING</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber"> 134 </span></span>
<span class="line"><span class="LineNumber"> 135 </span>    <span class="ResWord">def</span> <span class="ID">OnSortBySubmitter</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">evt</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber"> 136 </span>        <span class="ResWord">def</span> <span class="ID">compare_func</span><span class="Operator">(</span><span class="ID">row1</span><span class="Operator">,</span> <span class="ID">row2</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber"> 137 </span>            <span class="Comment"># compare the values in the 4th col of the data</span></span>
<span class="line"><span class="LineNumber"> 138 </span><span class="Comment"></span>            <span class="ID">val1</span> <span class="Operator">=</span> <span class="ID">data</span><span class="Operator">.</span><span class="ID">rows</span><span class="Operator">[</span><span class="ID">row1</span><span class="Operator">]</span><span class="Operator">[</span><span class="Number">3</span><span class="Operator">]</span></span>
<span class="line"><span class="LineNumber"> 139 </span>            <span class="ID">val2</span> <span class="Operator">=</span> <span class="ID">data</span><span class="Operator">.</span><span class="ID">rows</span><span class="Operator">[</span><span class="ID">row2</span><span class="Operator">]</span><span class="Operator">[</span><span class="Number">3</span><span class="Operator">]</span></span>
<span class="line"><span class="LineNumber"> 140 </span>            <span class="ResWord">if</span> <span class="ID">val1</span>   <span class="ID">val2</span><span class="Operator">:</span> <span class="ResWord">return</span> <span class="Operator">-</span><span class="Number">1</span></span>
<span class="line"><span class="LineNumber"> 141 </span>            <span class="ResWord">if</span> <span class="ID">val1</span>   <span class="ID">val2</span><span class="Operator">:</span> <span class="ResWord">return</span> <span class="Number">1</span></span>
<span class="line"><span class="LineNumber"> 142 </span>            <span class="ResWord">return</span> <span class="Number">0</span></span>
<span class="line"><span class="LineNumber"> 143 </span></span>
<span class="line"><span class="LineNumber"> 144 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">SortItems</span><span class="Operator">(</span><span class="ID">compare_func</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber"> 145 </span></span>
<span class="line"><span class="LineNumber"> 146 </span></span>
<span class="line"><span class="LineNumber"> 147 </span></span>
<span class="line"><span class="LineNumber"> 148 </span>    <span class="ResWord">def</span> <span class="ID">OnShowSelected</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">evt</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber"> 149 </span>        <span class="ResWord">print</span> <span class="String">"These items are selected:"</span></span>
<span class="line"><span class="LineNumber"> 150 </span>        <span class="ID">index</span> <span class="Operator">=</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">GetFirstSelected</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber"> 151 </span>        <span class="ResWord">if</span> <span class="ID">index</span> <span class="Operator">==</span> <span class="Operator">-</span><span class="Number">1</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber"> 152 </span>            <span class="ResWord">print</span> <span class="String">"\tNone"</span></span>
<span class="line"><span class="LineNumber"> 153 </span>            <span class="ResWord">return</span></span>
<span class="line"><span class="LineNumber"> 154 </span>        <span class="ResWord">while</span> <span class="ID">index</span> <span class="Operator">!=</span> <span class="Operator">-</span><span class="Number">1</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber"> 155 </span>            <span class="ID">item</span> <span class="Operator">=</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">GetItem</span><span class="Operator">(</span><span class="ID">index</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber"> 156 </span>            <span class="ResWord">print</span> <span class="String">"\t%s"</span> <span class="Operator">%</span> <span class="ID">item</span><span class="Operator">.</span><span class="ID">GetText</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber"> 157 </span>            <span class="ID">index</span> <span class="Operator">=</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">GetNextSelected</span><span class="Operator">(</span><span class="ID">index</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber"> 158 </span></span>
<span class="line"><span class="LineNumber"> 159 </span>    <span class="ResWord">def</span> <span class="ID">OnSelectAll</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">evt</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber"> 160 </span>        <span class="ResWord">for</span> <span class="ID">index</span> <span class="ResWord">in</span> <span class="ID">range</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">GetItemCount</span><span class="Operator">(</span><span class="Operator">)</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber"> 161 </span>            <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">Select</span><span class="Operator">(</span><span class="ID">index</span><span class="Operator">,</span> <span class="ID">True</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber"> 162 </span></span>
<span class="line"><span class="LineNumber"> 163 </span>    <span class="ResWord">def</span> <span class="ID">OnSelectNone</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">evt</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber"> 164 </span>        <span class="ID">index</span> <span class="Operator">=</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">GetFirstSelected</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber"> 165 </span>        <span class="ResWord">while</span> <span class="ID">index</span> <span class="Operator">!=</span> <span class="Operator">-</span><span class="Number">1</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber"> 166 </span>            <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">Select</span><span class="Operator">(</span><span class="ID">index</span><span class="Operator">,</span> <span class="ID">False</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber"> 167 </span>            <span class="ID">index</span> <span class="Operator">=</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">GetNextSelected</span><span class="Operator">(</span><span class="ID">index</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber"> 168 </span></span>
<span class="line"><span class="LineNumber"> 169 </span></span>
<span class="line"><span class="LineNumber"> 170 </span>    <span class="ResWord">def</span> <span class="ID">OnSetTextColour</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">evt</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber"> 171 </span>        <span class="ID">dlg</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">ColourDialog</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber"> 172 </span>        <span class="ResWord">if</span> <span class="ID">dlg</span><span class="Operator">.</span><span class="ID">ShowModal</span><span class="Operator">(</span><span class="Operator">)</span> <span class="Operator">==</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">ID_OK</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber"> 173 </span>            <span class="ID">colour</span> <span class="Operator">=</span> <span class="ID">dlg</span><span class="Operator">.</span><span class="ID">GetColourData</span><span class="Operator">(</span><span class="Operator">)</span><span class="Operator">.</span><span class="ID">GetColour</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber"> 174 </span>            <span class="ID">index</span> <span class="Operator">=</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">GetFirstSelected</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber"> 175 </span>            <span class="ResWord">while</span> <span class="ID">index</span> <span class="Operator">!=</span> <span class="Operator">-</span><span class="Number">1</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber"> 176 </span>                <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">SetItemTextColour</span><span class="Operator">(</span><span class="ID">index</span><span class="Operator">,</span> <span class="ID">colour</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber"> 177 </span>                <span class="ID">index</span> <span class="Operator">=</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">GetNextSelected</span><span class="Operator">(</span><span class="ID">index</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber"> 178 </span>        <span class="ID">dlg</span><span class="Operator">.</span><span class="ID">Destroy</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber"> 179 </span></span>
<span class="line"><span class="LineNumber"> 180 </span>    <span class="ResWord">def</span> <span class="ID">OnSetBGColour</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">evt</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber"> 181 </span>        <span class="ID">dlg</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">ColourDialog</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber"> 182 </span>        <span class="ResWord">if</span> <span class="ID">dlg</span><span class="Operator">.</span><span class="ID">ShowModal</span><span class="Operator">(</span><span class="Operator">)</span> <span class="Operator">==</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">ID_OK</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber"> 183 </span>            <span class="ID">colour</span> <span class="Operator">=</span> <span class="ID">dlg</span><span class="Operator">.</span><span class="ID">GetColourData</span><span class="Operator">(</span><span class="Operator">)</span><span class="Operator">.</span><span class="ID">GetColour</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber"> 184 </span>            <span class="ID">index</span> <span class="Operator">=</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">GetFirstSelected</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber"> 185 </span>            <span class="ResWord">while</span> <span class="ID">index</span> <span class="Operator">!=</span> <span class="Operator">-</span><span class="Number">1</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber"> 186 </span>                <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">SetItemBackgroundColour</span><span class="Operator">(</span><span class="ID">index</span><span class="Operator">,</span> <span class="ID">colour</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber"> 187 </span>                <span class="ID">index</span> <span class="Operator">=</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">GetNextSelected</span><span class="Operator">(</span><span class="ID">index</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber"> 188 </span>        <span class="ID">dlg</span><span class="Operator">.</span><span class="ID">Destroy</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber"> 189 </span></span>
<span class="line"><span class="LineNumber"> 190 </span></span>
<span class="line"><span class="LineNumber"> 191 </span>    <span class="ResWord">def</span> <span class="ID">OnEnableEditing</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">evt</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber"> 192 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">editable</span> <span class="Operator">=</span> <span class="ID">evt</span><span class="Operator">.</span><span class="ID">IsChecked</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber"> 193 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">MakeListCtrl</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber"> 194 </span></span>
<span class="line"><span class="LineNumber"> 195 </span>    <span class="ResWord">def</span> <span class="ID">OnEditItem</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">evt</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber"> 196 </span>        <span class="ID">index</span> <span class="Operator">=</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">GetFirstSelected</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber"> 197 </span>        <span class="ResWord">if</span> <span class="ID">index</span> <span class="Operator">!=</span> <span class="Operator">-</span><span class="Number">1</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber"> 198 </span>            <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">EditLabel</span><span class="Operator">(</span><span class="ID">index</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber"> 199 </span></span>
<span class="line"><span class="LineNumber"> 200 </span></span>
<span class="line"><span class="LineNumber"> 201 </span><span class="ResWord">class</span> <span class="ID">DemoApp</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">App</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber"> 202 </span>    <span class="ResWord">def</span> <span class="ID">OnInit</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber"> 203 </span>        <span class="ID">frame</span> <span class="Operator">=</span> <span class="ID">DemoFrame</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber"> 204 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">SetTopWindow</span><span class="Operator">(</span><span class="ID">frame</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber"> 205 </span>        <span class="ResWord">print</span> <span class="String">"Program output appears here..."</span></span>
<span class="line"><span class="LineNumber"> 206 </span>        <span class="ID">frame</span><span class="Operator">.</span><span class="ID">Show</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber"> 207 </span>        <span class="ResWord">return</span> <span class="ID">True</span></span>
<span class="line"><span class="LineNumber"> 208 </span></span>
<span class="line"><span class="LineNumber"> 209 </span><span class="ID">app</span> <span class="Operator">=</span> <span class="ID">DemoApp</span><span class="Operator">(</span><span class="ID">redirect</span><span class="Operator">=</span><span class="ID">True</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber"> 210 </span><span class="ID">app</span><span class="Operator">.</span><span class="ID">MainLoop</span><span class="Operator">(</span><span class="Operator">)</span><span class="Text"></span></span>
</pre>
</div>
<span id="line-714" class="anchor"></span><p class="line874">一旦你输入上面的代码并执行它，你将看到列表控件特性的演示，包括像项目的排序，这我们将在下一节讨论。 <span id="line-715" class="anchor"></span><span id="line-716" class="anchor"></span></p>
<p class="line874">在这一节，我们将讨论对列表控件中的项目进行编辑、排序和想找。 <span id="line-717" class="anchor"></span><span id="line-718" class="anchor"></span><span id="line-719" class="anchor"></span></p>
<p class="line867">
</p>
<h3 id="head-8650fe7721a7b06bafc509fdf158954452021277">3.3. 如何编辑标签？</h3>
<span id="line-720" class="anchor"></span><span id="line-721" class="anchor"></span><span id="line-722" class="anchor"></span><p class="line874">除了报告列表外，编辑一个列表中的项目是简单的，在报告列表中，用户只能编辑一行的第一个一。而对于其它的列表，则没有问题；每个项目的标准的标签都是可编辑的。 <span id="line-723" class="anchor"></span><span id="line-724" class="anchor"></span></p>
<p class="line862">要使一个列表是可编辑的，则当列表被创建时要在构造函数中包含样?奖昙莧{{wx.LC_EDIT_LABELS}}}。 <span id="line-725" class="anchor"></span><span id="line-726" class="anchor"></span></p>
<p class="line867"><tt>list = wx.ListCtrl(self, </tt>-1, <tt>style=wx.LC_REPORT </tt>| <tt>wx.LC_EDIT_LABELS)</tt> <span id="line-727" class="anchor"></span><span id="line-728" class="anchor"></span></p>
<p class="line862">如果这个编辑标记被设置了，那么用户就能够通过在一个已选择的列表项上敲击来开始一个编辑会话。编辑完后按下<tt>Enter</tt>键结束编辑会话，新的文本就变成了文本标签。在列表控件中的鼠标敲击也可结束编辑会话（一次只能有一个编辑会话）。按下<tt>Esc</tt>键则取消编辑会话，这样的话，新输入的文本就没有用了。 <span id="line-729" class="anchor"></span><span id="line-730" class="anchor"></span></p>
<p class="line874">下面的两个事件类型是由编辑会话触发的。 <span id="line-731" class="anchor"></span><span id="line-732" class="anchor"></span></p>
<p class="line862">* <tt>EVT_LIST_BEGIN_LABEL_EDIT </tt> <span id="line-733" class="anchor"></span><span id="line-734" class="anchor"></span></p>
<p class="line862">* <tt>EVT_LIST_END_LABEL_EDIT</tt> <span id="line-735" class="anchor"></span><span id="line-736" class="anchor"></span></p>
<p class="line862">记住，如果你想事件在被你的自定义的事件处理器处理后继续被处理，那么你需要在你的事件处理器中包括<tt>Skip()</tt>调用。当用户开始一个编辑会话时，一个<tt>EVT_LIST_BEGIN_LABEL_EDIT</tt>类型的列表事件被触发，当会话结束时（通过使用<tt>Enter</tt>或<tt>Esc</tt>），<tt>EVT_LIST_END_LABEL_EDIT</tt>类型的列表事件被触发。你可以否决(<tt>veto)</tt>编辑事件的开始，这样编辑会话就不会开始了。否决编辑事件的结束将阻止列表文本的改变。 <span id="line-737" class="anchor"></span><span id="line-738" class="anchor"></span></p>
<p class="line867"><tt>wx.ListEvent</tt>类有两个属性，这两个属性只在当处理一个<tt>EVT_LIST_END_LABEL_EDIT</tt>事件时才会用到。如果编辑结束并确认后，<tt>GetLabel()</tt>返回列表项目标签的新文本，如果编辑被<tt>Esc</tt>键取消了，那么<tt>GetLabel()</tt>返回一个空字符串。这意味着你不能使用<tt>GetLabel()</tt>来区别“取消”和“用户故意输入的空字符串标签”。如果必须的话，可以使用<tt>IsEditCancelled()</tt>，它在因取消而导致的编辑结束时返回<tt>True</tt>，否则返回<tt>False</tt>。
<span id="line-739" class="anchor"></span><span id="line-740" class="anchor"></span></p>
<p class="line862">如果你想通过其它的用户事件来启动一个编辑会话的话，你可以在程序中使用列表控件的<tt>EditLabel(item)</tt>方法来触发一个编辑。其中的<tt>item</tt>参数是是要被编辑的列表项的索引。该方法触发<tt>EVT_LIST_BEGIN_LABEL_EDIT</tt>事件。 <span id="line-741" class="anchor"></span><span id="line-742" class="anchor"></span></p>
<p class="line862">如果你愿意直接处理用于列表项编辑控件，你可以使用列表控件的方法<tt>GetEditControl()</tt>来得到该编辑控件。该方法返回用于当前编辑的文本控件。如果当前没有编辑，该方法返回<tt>None</tt>。目前该方法只工作于<tt>Windows</tt>操作系统下。 <span id="line-743" class="anchor"></span><span id="line-744" class="anchor"></span><span id="line-745" class="anchor"></span></p>
<p class="line867"><strong><tt>boa</tt>-<tt>constructor</tt>简介</strong> <span id="line-746" class="anchor"></span><span id="line-747" class="anchor"></span></p>
<p class="line867"><tt>boa</tt>-<tt>constructor</tt>是一个跨平台的<tt>Python</tt>集成开发环境和<tt>wxPython</tt>图形用户界面构建器。它提供了可视化方式的框架（窗口）的创建和处理、对象检视器(<tt>object inspector)</tt>、编辑器、继承的等级、<tt>html</tt>文档字符串、高级的调试器和集成化的帮助系统。俨然一个用于<tt>Python</tt>的<tt>Delphi</tt>。 <span id="line-748" class="anchor"></span><span id="line-749" class="anchor"></span></p>
<p class="line862">对的<tt>Zope</tt>支持：对象的创建和编辑。剪切，复制，粘贴，导入和导出。检视器(<tt>inspector)</tt>和<tt>Python</tt>脚本调试中的创建和编辑。 <span id="line-750" class="anchor"></span><span id="line-751" class="anchor"></span></p>
<p class="line867"><tt>boa</tt>-<tt>constructor</tt>是用<tt>Python</tt>和<tt>wxPython</tt>库写成的。使用它之前，你必须安装了<tt>wxPython
2.4.0.7</tt>或<tt>wxPython</tt>更高的版本以及<tt>Python 2.1</tt>或<tt>Python</tt>的更高的版本。建议在使用<tt>boa</tt>-<tt>constructor</tt>之前，先入门<tt>wxPython</tt>。 <span id="line-752" class="anchor"></span><span id="line-753" class="anchor"></span></p>
<p class="line867"><tt>boa</tt>-<tt>constructor</tt>项目位于<tt>SourceForge</tt>中。 下载<tt>boa</tt>-<tt>constructor  </tt> <span id="line-754" class="anchor"></span><span id="line-755" class="anchor"></span><span id="line-756" class="anchor"></span></p>
<p class="line867">
</p>
<h3 id="head-30617f689bbc2628e1fe91b0e174f48dad2e98e3">3.4. 如何对列表排序？</h3>
<span id="line-757" class="anchor"></span><span id="line-758" class="anchor"></span><span id="line-759" class="anchor"></span><p class="line862">在<tt>wxPython</tt>中有三个有用的方法可以对列表进行排序，在这一节，我们将按照从易到难的顺序来讨论。 <span id="line-760" class="anchor"></span><span id="line-761" class="anchor"></span><span id="line-762" class="anchor"></span></p>
<p class="line867"><strong>在创建的时候告诉列表去排序</strong> <span id="line-763" class="anchor"></span><span id="line-764" class="anchor"></span></p>
<p class="line862">对一个列表控件排序的最容易的方法，是在构造函数中告诉该列表控件对项目进行排序。你可以通过使用样式标记<tt>wx.LC_SORT_ASCENDING</tt>或<tt>wx.LC_SORT_DESCENDING</tt>来实现。这两个标记导致了列表在初始显示的时候被排序，并且在<tt>Windows</tt>上，当新的项目被添加时，依然遵循所样式标记来排序。对于每个列表项的数据的排序，是基于其字符串文本的，只是简单的对字符串进行比较。如果列表是报告模式的，则排序是基于每行的最左边的列的字符串的。
<span id="line-765" class="anchor"></span><span id="line-766" class="anchor"></span><span id="line-767" class="anchor"></span></p>
<p class="line867"><strong>基于数据而非所显示的文本来排序</strong> <span id="line-768" class="anchor"></span><span id="line-769" class="anchor"></span></p>
<p class="line862">有时，你想根据其它方面而非列表标签的字符串来对列表排序。在<tt>wxPython</tt>中，你可以做到这一点，但这是较为复杂的。首先，你需要为列表中的每个项目设置项目数据，这通过使用<tt>SetItemData(item, data)</tt>方法。参数<tt>item</tt>是项目在未排序的列表中的索引，参数<tt>data</tt>必须是一个整形或长整形的值（由于C++的数据类型的限制），这就有点限制了该机制的作用。如果要获取某行的项目数据，可以使用方法<tt>GetItemData(item)</tt>。
<span id="line-770" class="anchor"></span><span id="line-771" class="anchor"></span></p>
<p class="line862">一旦你设置了项目数据，你就可以使用方法<tt>SortItems(func)</tt>来排序项目。参数<tt>func</tt>是一个可调用的<tt>Python</tt>对象（函数），它需要两个整数。<tt>func</tt>函数对两个列表项目的数据进行比较——你不能得到行自身的引用。如果第一项比第二项大的话，函数将返回一个正整数，如果第一项比第二项小的话，返回一个负值，如果相等则返回0。尽管实现这个函数的最显而易见的方法是只对这两个项目做一个数字的比较就可以了，但是这并不唯一的排序方法。比如，数据项可能是外部字典或列表中的一个关键字，与该关键字相应的是一个更复杂的数据项，这种情况下，你可以通过比较与该关键字相应的数据项来排序。
<span id="line-772" class="anchor"></span><span id="line-773" class="anchor"></span><span id="line-774" class="anchor"></span></p>
<p class="line867"><strong>使用<tt>mixin</tt>类进行列排序</strong> <span id="line-775" class="anchor"></span><span id="line-776" class="anchor"></span></p>
<p class="line862">关于对一个列表控件进行排序的常见的情况是，让用户能够通过在报告模式的列表的任一列上进行敲击来根据该列进行排序。你可以使用<tt>SortItems()</tt>机制来实现，但是它在保持到列的跟踪方面有点复杂。幸运的是，一个名为<tt>ColumnSorterMixin</tt>的<tt>wxPython</tt>的<tt>mixin</tt>类可以为你处理这些信息，它位于<tt>wx.lib.mixins.listctrl</tt>模块中。图13.5显示了使用该<tt>mixin</tt>类对列进行的排序。 <span id="line-777" class="anchor"></span><span id="line-778" class="anchor"></span><span id="line-779" class="anchor"></span></p>
<p class="line867"><strong>图13.5</strong> <span id="line-780" class="anchor"></span><span id="line-781" class="anchor"></span></p>
<p class="line867"><img title="attachment:w13.5.gif" src="/moin/WxPythonInAction/ChapterThirteen?action=AttachFile&amp;do=get&amp;target=w13.5.gif" class="attachment"><span id="line-782" class="anchor"></span><span id="line-783" class="anchor"></span><span id="line-784" class="anchor"></span></p>
<p class="line862">声明这个<tt>mixin</tt>就和<tt>Python</tt>中声明任何其它的多重继承一样，如下所示： <span id="line-785" class="anchor"></span><span id="line-786" class="anchor"></span><span id="line-787" class="anchor"></span><span id="line-788" class="anchor"></span><span id="line-789" class="anchor"></span><span id="line-790" class="anchor"></span><span id="line-791" class="anchor"></span><span id="line-792" class="anchor"></span><span id="line-793" class="anchor"></span><span id="line-794" class="anchor"></span></p>
<div lang="en" dir="ltr" class="codearea">
<script type="text/javascript">
document.write('<a href="#" onclick="return togglenumber(\'CA-3bfc7c2c042d1a7c51cf51a3ac56dd5993b8410e_007\', 1, 1);" \
class="codenumbers">切换行号显示<\/a>');
</script><a class="codenumbers" onclick="return togglenumber('CA-3bfc7c2c042d1a7c51cf51a3ac56dd5993b8410e_007', 1, 1);" href="#">切换行号显示</a>
<pre lang="en" id="CA-3bfc7c2c042d1a7c51cf51a3ac56dd5993b8410e_007" dir="ltr"><span class="line"><span class="LineNumber">   1 </span><span class="ResWord">import</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">lib</span><span class="Operator">.</span><span class="ID">mixins</span><span class="Operator">.</span><span class="ID">listctrl</span> <span class="ResWord">as</span> <span class="ID">listmix</span></span>
<span class="line"><span class="LineNumber">   2 </span></span>
<span class="line"><span class="LineNumber">   3 </span><span class="ResWord">class</span> <span class="ID">ListCtrlPanel</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">Panel</span><span class="Operator">,</span> <span class="ID">listmix</span><span class="Operator">.</span><span class="ID">ColumnSorterMixin</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">   4 </span>    <span class="ResWord">def</span> <span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">parent</span><span class="Operator">,</span> <span class="ID">log</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">   5 </span>        <span class="ID">wx</span><span class="Operator">.</span><span class="ID">Panel</span><span class="Operator">.</span><span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">parent</span><span class="Operator">,</span> <span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span> <span class="ID">style</span><span class="Operator">=</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">WANTS_CHARS</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   6 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span> <span class="Operator">=</span> <span class="ID">TestListCtrl</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">tID</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   7 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">itemDataMap</span> <span class="Operator">=</span> <span class="ID">musicdata</span></span>
<span class="line"><span class="LineNumber">   8 </span>        <span class="ID">listmix</span><span class="Operator">.</span><span class="ID">ColumnSorterMixin</span><span class="Operator">.</span><span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="Number">3</span><span class="Operator">)</span><span class="Text"></span></span>
</pre>
</div>
<span id="line-795" class="anchor"></span><span id="line-796" class="anchor"></span><p class="line874">例13.4是图13.5的例子代码 <span id="line-797" class="anchor"></span><span id="line-798" class="anchor"></span><span id="line-799" class="anchor"></span></p>
<p class="line867"><strong>例13.4　使用<tt>mixin</tt>对一个报告列表进行排序</strong> <span id="line-800" class="anchor"></span><span id="line-801" class="anchor"></span><span id="line-802" class="anchor"></span><span id="line-803" class="anchor"></span><span id="line-804" class="anchor"></span><span id="line-805" class="anchor"></span><span id="line-806" class="anchor"></span><span id="line-807" class="anchor"></span><span id="line-808" class="anchor"></span><span id="line-809" class="anchor"></span><span id="line-810" class="anchor"></span><span id="line-811" class="anchor"></span><span id="line-812" class="anchor"></span><span id="line-813" class="anchor"></span><span id="line-814" class="anchor"></span><span id="line-815" class="anchor"></span><span id="line-816" class="anchor"></span><span id="line-817" class="anchor"></span><span id="line-818" class="anchor"></span><span id="line-819" class="anchor"></span><span id="line-820" class="anchor"></span><span id="line-821" class="anchor"></span><span id="line-822" class="anchor"></span><span id="line-823" class="anchor"></span><span id="line-824" class="anchor"></span><span id="line-825" class="anchor"></span><span id="line-826" class="anchor"></span><span id="line-827" class="anchor"></span><span id="line-828" class="anchor"></span><span id="line-829" class="anchor"></span><span id="line-830" class="anchor"></span><span id="line-831" class="anchor"></span><span id="line-832" class="anchor"></span><span id="line-833" class="anchor"></span><span id="line-834" class="anchor"></span><span id="line-835" class="anchor"></span><span id="line-836" class="anchor"></span><span id="line-837" class="anchor"></span><span id="line-838" class="anchor"></span><span id="line-839" class="anchor"></span><span id="line-840" class="anchor"></span><span id="line-841" class="anchor"></span><span id="line-842" class="anchor"></span><span id="line-843" class="anchor"></span><span id="line-844" class="anchor"></span><span id="line-845" class="anchor"></span><span id="line-846" class="anchor"></span><span id="line-847" class="anchor"></span><span id="line-848" class="anchor"></span><span id="line-849" class="anchor"></span><span id="line-850" class="anchor"></span><span id="line-851" class="anchor"></span><span id="line-852" class="anchor"></span><span id="line-853" class="anchor"></span><span id="line-854" class="anchor"></span><span id="line-855" class="anchor"></span><span id="line-856" class="anchor"></span><span id="line-857" class="anchor"></span><span id="line-858" class="anchor"></span><span id="line-859" class="anchor"></span><span id="line-860" class="anchor"></span><span id="line-861" class="anchor"></span><span id="line-862" class="anchor"></span><span id="line-863" class="anchor"></span><span id="line-864" class="anchor"></span><span id="line-865" class="anchor"></span><span id="line-866" class="anchor"></span><span id="line-867" class="anchor"></span><span id="line-868" class="anchor"></span><span id="line-869" class="anchor"></span><span id="line-870" class="anchor"></span><span id="line-871" class="anchor"></span><span id="line-872" class="anchor"></span><span id="line-873" class="anchor"></span><span id="line-874" class="anchor"></span><span id="line-875" class="anchor"></span></p>
<div lang="en" dir="ltr" class="codearea">
<script type="text/javascript">
document.write('<a href="#" onclick="return togglenumber(\'CA-2581c18b5d76c85808878159ab76f17c13d1d4bb_008\', 1, 1);" \
class="codenumbers">切换行号显示<\/a>');
</script><a class="codenumbers" onclick="return togglenumber('CA-2581c18b5d76c85808878159ab76f17c13d1d4bb_008', 1, 1);" href="#">切换行号显示</a>
<pre lang="en" id="CA-2581c18b5d76c85808878159ab76f17c13d1d4bb_008" dir="ltr"><span class="line"><span class="LineNumber">   1 </span><span class="Comment">#!/usr/bin/python</span></span>
<span class="line"><span class="LineNumber">   2 </span><span class="Comment"></span><span class="Comment">#-*- encoding:UTF-8 -*-</span></span>
<span class="line"><span class="LineNumber">   3 </span><span class="Comment"></span><span class="ResWord">import</span> <span class="ID">wx</span></span>
<span class="line"><span class="LineNumber">   4 </span><span class="ResWord">import</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">lib</span><span class="Operator">.</span><span class="ID">mixins</span><span class="Operator">.</span><span class="ID">listctrl</span></span>
<span class="line"><span class="LineNumber">   5 </span><span class="ResWord">import</span> <span class="ID">sys</span><span class="Operator">,</span> <span class="ID">glob</span><span class="Operator">,</span> <span class="ID">random</span></span>
<span class="line"><span class="LineNumber">   6 </span><span class="ResWord">import</span> <span class="ID">data</span></span>
<span class="line"><span class="LineNumber">   7 </span></span>
<span class="line"><span class="LineNumber">   8 </span><span class="ResWord">class</span> <span class="ID">DemoFrame</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">Frame</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">lib</span><span class="Operator">.</span><span class="ID">mixins</span><span class="Operator">.</span><span class="ID">listctrl</span><span class="Operator">.</span><span class="ID">ColumnSorterMixin</span><span class="Operator">)</span><span class="Operator">:</span><span class="Comment">#多重继承</span></span>
<span class="line"><span class="LineNumber">   9 </span>    <span class="ResWord">def</span> <span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  10 </span>        <span class="ID">wx</span><span class="Operator">.</span><span class="ID">Frame</span><span class="Operator">.</span><span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">None</span><span class="Operator">,</span> <span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">  11 </span>                          <span class="String">"wx.ListCtrl with ColumnSorterMixin"</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">  12 </span>                          <span class="ID">size</span><span class="Operator">=</span><span class="Operator">(</span><span class="Number">600</span><span class="Operator">,</span><span class="Number">400</span><span class="Operator">)</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  13 </span></span>
<span class="line"><span class="LineNumber">  14 </span>        <span class="Comment"># load some images into an image list</span></span>
<span class="line"><span class="LineNumber">  15 </span><span class="Comment"></span>        <span class="ID">il</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">ImageList</span><span class="Operator">(</span><span class="Number">16</span><span class="Operator">,</span><span class="Number">16</span><span class="Operator">,</span> <span class="ID">True</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  16 </span>        <span class="ResWord">for</span> <span class="ID">name</span> <span class="ResWord">in</span> <span class="ID">glob</span><span class="Operator">.</span><span class="ID">glob</span><span class="Operator">(</span><span class="String">"smicon??.png"</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  17 </span>            <span class="ID">bmp</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">Bitmap</span><span class="Operator">(</span><span class="ID">name</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">BITMAP_TYPE_PNG</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  18 </span>            <span class="ID">il_max</span> <span class="Operator">=</span> <span class="ID">il</span><span class="Operator">.</span><span class="ID">Add</span><span class="Operator">(</span><span class="ID">bmp</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  19 </span></span>
<span class="line"><span class="LineNumber">  20 </span>        <span class="Comment"># add some arrows for the column sorter</span></span>
<span class="line"><span class="LineNumber">  21 </span><span class="Comment"></span><span class="Comment">#　添加箭头到图像列表</span></span>
<span class="line"><span class="LineNumber">  22 </span><span class="Comment"></span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">up</span> <span class="Operator">=</span> <span class="ID">il</span><span class="Operator">.</span><span class="ID">AddWithColourMask</span><span class="Operator">(</span></span>
<span class="line"><span class="LineNumber">  23 </span>            <span class="ID">wx</span><span class="Operator">.</span><span class="ID">Bitmap</span><span class="Operator">(</span><span class="String">"sm_up.bmp"</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">BITMAP_TYPE_BMP</span><span class="Operator">)</span><span class="Operator">,</span> <span class="String">"blue"</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  24 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">dn</span> <span class="Operator">=</span> <span class="ID">il</span><span class="Operator">.</span><span class="ID">AddWithColourMask</span><span class="Operator">(</span></span>
<span class="line"><span class="LineNumber">  25 </span>            <span class="ID">wx</span><span class="Operator">.</span><span class="ID">Bitmap</span><span class="Operator">(</span><span class="String">"sm_down.bmp"</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">BITMAP_TYPE_BMP</span><span class="Operator">)</span><span class="Operator">,</span> <span class="String">"blue"</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  26 </span></span>
<span class="line"><span class="LineNumber">  27 </span>        <span class="Comment"># create the list control</span></span>
<span class="line"><span class="LineNumber">  28 </span><span class="Comment"></span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">ListCtrl</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span> <span class="ID">style</span><span class="Operator">=</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">LC_REPORT</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  29 </span></span>
<span class="line"><span class="LineNumber">  30 </span>        <span class="Comment"># assign the image list to it</span></span>
<span class="line"><span class="LineNumber">  31 </span><span class="Comment"></span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">AssignImageList</span><span class="Operator">(</span><span class="ID">il</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">IMAGE_LIST_SMALL</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  32 </span></span>
<span class="line"><span class="LineNumber">  33 </span>        <span class="Comment"># Add some columns</span></span>
<span class="line"><span class="LineNumber">  34 </span><span class="Comment"></span>        <span class="ResWord">for</span> <span class="ID">col</span><span class="Operator">,</span> <span class="ID">text</span> <span class="ResWord">in</span> <span class="ID">enumerate</span><span class="Operator">(</span><span class="ID">data</span><span class="Operator">.</span><span class="ID">columns</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  35 </span>            <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">InsertColumn</span><span class="Operator">(</span><span class="ID">col</span><span class="Operator">,</span> <span class="ID">text</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  36 </span></span>
<span class="line"><span class="LineNumber">  37 </span>        <span class="Comment"># add the rows</span></span>
<span class="line"><span class="LineNumber">  38 </span><span class="Comment"></span><span class="Comment">#　创建数据映射</span></span>
<span class="line"><span class="LineNumber">  39 </span><span class="Comment"></span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">itemDataMap</span> <span class="Operator">=</span> <span class="Operator">{</span><span class="Operator">}</span></span>
<span class="line"><span class="LineNumber">  40 </span>        <span class="ResWord">for</span> <span class="ID">item</span> <span class="ResWord">in</span> <span class="ID">data</span><span class="Operator">.</span><span class="ID">rows</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  41 </span>            <span class="ID">index</span> <span class="Operator">=</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">InsertStringItem</span><span class="Operator">(</span><span class="ID">sys</span><span class="Operator">.</span><span class="ID">maxint</span><span class="Operator">,</span> <span class="ID">item</span><span class="Operator">[</span><span class="Number">0</span><span class="Operator">]</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  42 </span>            <span class="ResWord">for</span> <span class="ID">col</span><span class="Operator">,</span> <span class="ID">text</span> <span class="ResWord">in</span> <span class="ID">enumerate</span><span class="Operator">(</span><span class="ID">item</span><span class="Operator">[</span><span class="Number">1</span><span class="Operator">:</span><span class="Operator">]</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  43 </span>                <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">SetStringItem</span><span class="Operator">(</span><span class="ID">index</span><span class="Operator">,</span> <span class="ID">col</span><span class="Operator">+</span><span class="Number">1</span><span class="Operator">,</span> <span class="ID">text</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  44 </span></span>
<span class="line"><span class="LineNumber">  45 </span>            <span class="Comment"># give each item a data value, and map it back to the</span></span>
<span class="line"><span class="LineNumber">  46 </span><span class="Comment"></span>            <span class="Comment"># item values, for the column sorter</span></span>
<span class="line"><span class="LineNumber">  47 </span><span class="Comment"></span>            <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">SetItemData</span><span class="Operator">(</span><span class="ID">index</span><span class="Operator">,</span> <span class="ID">index</span><span class="Operator">)</span><span class="Comment">#　关联数据和映射</span></span>
<span class="line"><span class="LineNumber">  48 </span>            <span class="ID">self</span><span class="Operator">.</span><span class="ID">itemDataMap</span><span class="Operator">[</span><span class="ID">index</span><span class="Operator">]</span> <span class="Operator">=</span> <span class="ID">item</span></span>
<span class="line"><span class="LineNumber">  49 </span></span>
<span class="line"><span class="LineNumber">  50 </span>            <span class="Comment"># give each item a random image</span></span>
<span class="line"><span class="LineNumber">  51 </span><span class="Comment"></span>            <span class="ID">img</span> <span class="Operator">=</span> <span class="ID">random</span><span class="Operator">.</span><span class="ID">randint</span><span class="Operator">(</span><span class="Number">0</span><span class="Operator">,</span> <span class="ID">il_max</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  52 </span>            <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">SetItemImage</span><span class="Operator">(</span><span class="ID">index</span><span class="Operator">,</span> <span class="ID">img</span><span class="Operator">,</span> <span class="ID">img</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  53 </span></span>
<span class="line"><span class="LineNumber">  54 </span>        <span class="Comment"># set the width of the columns in various ways</span></span>
<span class="line"><span class="LineNumber">  55 </span><span class="Comment"></span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">SetColumnWidth</span><span class="Operator">(</span><span class="Number">0</span><span class="Operator">,</span> <span class="Number">120</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  56 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">SetColumnWidth</span><span class="Operator">(</span><span class="Number">1</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">LIST_AUTOSIZE</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  57 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">SetColumnWidth</span><span class="Operator">(</span><span class="Number">2</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">LIST_AUTOSIZE</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  58 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span><span class="Operator">.</span><span class="ID">SetColumnWidth</span><span class="Operator">(</span><span class="Number">3</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">LIST_AUTOSIZE_USEHEADER</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  59 </span></span>
<span class="line"><span class="LineNumber">  60 </span>        <span class="Comment"># initialize the column sorter</span></span>
<span class="line"><span class="LineNumber">  61 </span><span class="Comment"></span>        <span class="ID">wx</span><span class="Operator">.</span><span class="ID">lib</span><span class="Operator">.</span><span class="ID">mixins</span><span class="Operator">.</span><span class="ID">listctrl</span><span class="Operator">.</span><span class="ID">ColumnSorterMixin</span><span class="Operator">.</span><span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">  62 </span>                                                          <span class="ID">len</span><span class="Operator">(</span><span class="ID">data</span><span class="Operator">.</span><span class="ID">columns</span><span class="Operator">)</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  63 </span></span>
<span class="line"><span class="LineNumber">  64 </span>    <span class="ResWord">def</span> <span class="ID">GetListCtrl</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  65 </span>        <span class="ResWord">return</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span></span>
<span class="line"><span class="LineNumber">  66 </span></span>
<span class="line"><span class="LineNumber">  67 </span>    <span class="ResWord">def</span> <span class="ID">GetSortImages</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  68 </span>        <span class="ResWord">return</span> <span class="Operator">(</span><span class="ID">self</span><span class="Operator">.</span><span class="ID">dn</span><span class="Operator">,</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">up</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  69 </span></span>
<span class="line"><span class="LineNumber">  70 </span></span>
<span class="line"><span class="LineNumber">  71 </span><span class="ID">app</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">PySimpleApp</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  72 </span><span class="ID">frame</span> <span class="Operator">=</span> <span class="ID">DemoFrame</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  73 </span><span class="ID">frame</span><span class="Operator">.</span><span class="ID">Show</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  74 </span><span class="ID">app</span><span class="Operator">.</span><span class="ID">MainLoop</span><span class="Operator">(</span><span class="Operator">)</span><span class="Text"></span></span>
</pre>
</div>
<span id="line-876" class="anchor"></span><span id="line-877" class="anchor"></span><p class="line862">为了使用该<tt>mixin</tt>工作，你需要执行下面的东西： <span id="line-878" class="anchor"></span><span id="line-879" class="anchor"></span></p>
<p class="line862">1、扩展自<tt>ColumnSorterMixin</tt>的类（这里是<tt>DemoFrame</tt>）必须有一个名为<tt>GetListCtrl()</tt>的方法，它返回实际要被排序的列表控件。该方法被这个<tt>mixin</tt>用来得到控件的一个索引。 <span id="line-880" class="anchor"></span><span id="line-881" class="anchor"></span></p>
<p class="line862">2、在扩展自<tt>ColumnSorterMixin</tt>的类（这里是<tt>DemoFrame</tt>）的<tt>__init__()</tt>方法中，在你调用<tt>ColumnSorterMixin</tt>的<tt>__init__()</tt>方法之前，你必须创建<tt>GetListCtrl()</tt>所要引用的列表控件。该<tt>mixin</tt>的<tt>__init__()</tt>方法要求一个代表列表控?械牧泻诺恼怠? <span id="line-882" class="anchor"></span>3、你必须使用<tt>SetItemData()</tt>为列表中的每行设置一个唯一的数据值。 <span id="line-883" class="anchor"></span><span id="line-884" class="anchor"></span></p>
<p class="line862">4、扩展自<tt>ColumnSorterMixin</tt>的类（这里是<tt>DemoFrame</tt>）必须有一个名为<tt>itemDataMap</tt>的属性。该属性必须是一个字典。字典中的关键性的东西是由<tt>SetItemData()</tt>设置的数据值。这些值是你想用来对每列进行排序的值的一个元组。（典型情况下，这些值将是每列中的文本）。按句话说，<tt>itemDataMap</tt>本质上是将控件中的数据复制成另一种易于排序的形式。 <span id="line-885" class="anchor"></span><span id="line-886" class="anchor"></span></p>
<p class="line862">在<tt>ColumnSorterMixin</tt>的通常用法中，你要么创建<tt>itemDataMap</tt>用来添加项目到你的列表控件，要么你首先创建<tt>itemDataMap</tt>，并用它来建造列表控件本身。 <span id="line-887" class="anchor"></span><span id="line-888" class="anchor"></span></p>
<p class="line862">尽管配置可能有点复杂，但<tt>ColumnSorterMixin</tt>对于列的排序是一个不错的选择。 <span id="line-889" class="anchor"></span><span id="line-890" class="anchor"></span><span id="line-891" class="anchor"></span></p>
<p class="line867">
</p>
<h3 id="head-1075dd7ea6e8e571b5fe2e9441bac4b1ab0a7706">3.5. 进一步了解列表控件</h3>
<span id="line-892" class="anchor"></span><span id="line-893" class="anchor"></span><span id="line-894" class="anchor"></span><p class="line874">有时候，在你的程序中的某处你需要确定列表中的哪个项目被选择了，或者你需要通过编程来改变当前的选择以响应用户事件，或其它发生在你的程序中的一些事情。 <span id="line-895" class="anchor"></span><span id="line-896" class="anchor"></span></p>
<p class="line874">有几个与查找列表中的一个项目的索引相关的方法，它们提供了项目的一些信息，如表13.7所示。 <span id="line-897" class="anchor"></span><span id="line-898" class="anchor"></span></p>
<p class="line862">表13.8显示出了由<tt>HitTest()</tt>方法返回的可能的标记。实际应用中，可能返回不止一个标记。 <span id="line-899" class="anchor"></span><span id="line-900" class="anchor"></span><span id="line-901" class="anchor"></span></p>
<p class="line867"><strong>表13.7</strong>  <span id="line-902" class="anchor"></span><strong>查找列表中的项目的方法</strong> <span id="line-903" class="anchor"></span><span id="line-904" class="anchor"></span></p>
<div><table><tbody>
<tr>
<td><p class="line891"><tt>FindItem(start, str, partial=False)</tt></p></td>
<td><p class="line862">查找第一个与<tt>str</tt>匹配的项目。如果<tt>start</tt>为－1，那么搜索从头开始，否则搜索从<tt>start</tt>的指定的索引处开始。如果<tt>partial</tt>为<tt>True</tt>，那么这个匹配是匹配以<tt>str</tt>头的字符串，而非完全匹配。返回值是所匹配的字符串的索引。</p></td>
</tr>
<tr>
<td>
<span id="line-905" class="anchor"></span><p class="line891"><tt>FindItemAtPos(start</tt>，<tt>point, direction)</tt></p>
</td>
<td><p class="line862">查找与最接近位置点<tt>point</tt>的项目，<tt>point</tt>是一个<tt>wx.Point</tt>，它是相对于列表控件左上角的位置。参数<tt>direction</tt>是查找进行的方向。可能的取值有 <tt>wx.LIST_FIND_DOWN, wx.LIST_FIND_LEFT, wx.LIST_FIND_RIGHT, </tt>和<tt>wx.LIST_FIND_UP</tt>。</p></td>
</tr>
<tr>
<td>
<span id="line-906" class="anchor"></span><p class="line891"><tt>FindItemData(start,data)</tt></p>
</td>
<td><p class="line862">查找项目数据（使用<tt>SetItemData()</tt>设置的）与参数<tt>data</tt>匹配的项目。参数<tt>start</tt>同<tt>FindItem()</tt>。</p></td>
</tr>
<tr>
<td>
<span id="line-907" class="anchor"></span><p class="line891"><tt>HitTest(point)</tt></p>
</td>
<td><p class="line862">返回一个(<tt>index, flags)</tt>形式的<tt>Python</tt>元组。其中，<tt>index</tt>是项目在列表控件中的索引，如果没有所匹配的项目，那么<tt>index</tt>为－1。<tt>flags</tt>包含了关于位置点和项目的进一步的信息。<tt>flags</tt>是一个位掩码，其取值说明在表13.8中。</p></td>
</tr>
</tbody></table></div>
<span id="line-908" class="anchor"></span><span id="line-909" class="anchor"></span><span id="line-910" class="anchor"></span><p class="line867"><strong>表13.8</strong>  <span id="line-911" class="anchor"></span><strong>关于<tt>HitTest()</tt>方法返回值中的标记</strong> <span id="line-912" class="anchor"></span><span id="line-913" class="anchor"></span></p>
<div><table><tbody>
<tr>
<td><p class="line891"><tt>wx.LIST_HITTEST_ABOVE</tt></p></td>
<td><p class="line862">位置点在列表的客户区域的上面。</p></td>
</tr>
<tr>
<td>
<span id="line-914" class="anchor"></span><p class="line891"><tt>wx.LIST_HITTEST_BELOW</tt></p>
</td>
<td><p class="line862">位置点在列表的客户区域的下面。</p></td>
</tr>
<tr>
<td>
<span id="line-915" class="anchor"></span><p class="line891"><tt>wx.LIST_HITTEST_NOWhere</tt></p>
</td>
<td><p class="line862">位置点在列表的客户区域中，但不属于任何项目的部分。通常这是因为它是在列表的结尾处。</p></td>
</tr>
<tr>
<td>
<span id="line-916" class="anchor"></span><p class="line891"><tt>wx.LIST_HITTEST_ONITEM</tt></p>
</td>
<td><p class="line862">位置点在项目的矩形区域中，(<tt>index, flags)</tt>中的<tt>index</tt>是该项目的索引。</p></td>
</tr>
<tr>
<td>
<span id="line-917" class="anchor"></span><p class="line891"><tt>wx.LIST_HITTEST_ONITEMICON</tt></p>
</td>
<td><p class="line862">位置点在项目的图标区域中，(<tt>index, flags)</tt>中的<tt>index</tt>是该项目的索引。</p></td>
</tr>
<tr>
<td>
<span id="line-918" class="anchor"></span><p class="line891"><tt>wx.LIST_HITTEST_ONITEMLABEL</tt></p>
</td>
<td><p class="line862">位置点在项目的标签区域中，(<tt>index, flags)</tt>中的<tt>index</tt>是该项目的索引。</p></td>
</tr>
<tr>
<td>
<span id="line-919" class="anchor"></span><p class="line891"><tt>wx.LIST_HITTEST_ONITEMRIGHT</tt></p>
</td>
<td><p class="line862">位置点在项目右边的空白区域中。</p></td>
</tr>
<tr>
<td>
<span id="line-920" class="anchor"></span><p class="line891"><tt>wx.LIST_HITTEST_ONITEMSTATEICON</tt></p>
</td>
<td><p class="line862">位置点在一个项目的状态图标中。我们这里假设列表是树形的模式，并且存在一个用户定义的状态。</p></td>
</tr>
<tr>
<td>
<span id="line-921" class="anchor"></span><p class="line891"><tt>wx.LIST_HITTEST_TOLEFT</tt></p>
</td>
<td><p class="line862">位置点在列表的客户区域的左边。</p></td>
</tr>
<tr>
<td>
<span id="line-922" class="anchor"></span><p class="line891"><tt>wx.LIST_HITTEST_TORIGHT</tt></p>
</td>
<td><p class="line862">位置点在列表的客户区域的右边。</p></td>
</tr>
</tbody></table></div>
<span id="line-923" class="anchor"></span><span id="line-924" class="anchor"></span><p class="line862">至于其它的方面，还有几个方法，它们将为你提供关于所指定的项目的一些信息。方法<tt>GetItem()</tt>和<tt>GetItemText()</tt>方法我们早先已说过了，其它的见表13.9 <span id="line-925" class="anchor"></span><span id="line-926" class="anchor"></span><span id="line-927" class="anchor"></span></p>
<p class="line867"><strong>表13.9</strong>  <span id="line-928" class="anchor"></span><strong>获得列表控件的项目信息的方法</strong> <span id="line-929" class="anchor"></span><span id="line-930" class="anchor"></span></p>
<div><table><tbody>
<tr>
<td><p class="line891"><tt>GetItemPosition(item)</tt></p></td>
<td><p class="line862">返回一个<tt>wx.Point</tt>，它是指定项目的位置。只用于图标或小图标模式。所返回的位置点是该项目位置的左上角。</p></td>
</tr>
<tr>
<td>
<span id="line-931" class="anchor"></span><p class="line891"><tt>GetItemRect(item,code= wx.LIST_RECT_BOUNDS)</tt></p>
</td>
<td><p class="line862">返回<tt>item</tt>所指定的项目的矩形区域</p></td>
<td><p class="line862"> <tt>wx.Rect</tt>。参数<tt>code</tt>是可选的。<tt>code</tt>的默认值是<tt>wx.LIST_RECT_BOUNDS</tt>，这使得<tt>wxPython</tt>返回项目的整个矩形区域。<tt>code</tt>的其它取值还有</p></td>
<td><p class="line891"><tt>wx.LIST_RECT_ICON</tt>，它导致返回的只是项目的图标部分的矩形区域，<tt>wx.LIST_RECT_LABEL</tt>，它导致返回的只是项目的标签部分的矩形区域。</p></td>
</tr>
<tr>
<td>
<span id="line-932" class="anchor"></span><p class="line891"><tt>GetNextItem(item, geometry=wx.LIST_ALL, state=wx.LIST_STATE_DONTCARE )</tt></p>
</td>
<td><p class="line862">根据<tt>geometry</tt>和<tt>state</tt>参数，返回列表中位于<tt>item</tt>所指定的项目之后的下一个项目。其中的<tt>geometry</tt>和<tt>state</tt>参数，它们都有自己的取值，后面的列表将有说明。</p></td>
</tr>
<tr>
<td>
<span id="line-933" class="anchor"></span><p class="line891"><tt>SetItemPosition(item, pos)</tt></p>
</td>
<td><p class="line862">将<tt>item</tt>所指定的项目移动到<tt>pos</tt>所指定的位置处。只对图标或小图标模式的列表有意义。</p></td>
</tr>
</tbody></table></div>
<span id="line-934" class="anchor"></span><span id="line-935" class="anchor"></span><p class="line862">表13.10列出了用于<tt>GetNextItem()</tt>的<tt>geometry</tt>参数的取值。<tt>geometry</tt>参数只用于微软<tt>Windows</tt>下。 <span id="line-936" class="anchor"></span><span id="line-937" class="anchor"></span><span id="line-938" class="anchor"></span></p>
<p class="line867"><strong>表13.10</strong>  <span id="line-939" class="anchor"></span><strong><tt>GetNextItem()</tt>的<tt>geometry</tt>参数的取值</strong> <span id="line-940" class="anchor"></span><span id="line-941" class="anchor"></span></p>
<div><table><tbody>
<tr>
<td><p class="line891"><tt>wx.LIST_NEXT_ABOVE</tt></p></td>
<td><p class="line862">查找显示上位于开始项目之上的下一个为指定状态的项目。</p></td>
</tr>
<tr>
<td>
<span id="line-942" class="anchor"></span><p class="line891"><tt>wx.LIST_NEXT_ALL</tt></p>
</td>
<td><p class="line862">在列表中按索引的顺序查找下一个为指定状态的项目。</p></td>
</tr>
<tr>
<td>
<span id="line-943" class="anchor"></span><p class="line891"><tt>wx.LIST_NEXT_BELOW</tt></p>
</td>
<td><p class="line862">查找显示上位于开始项目之下的下一个为指定状态的项目。</p></td>
</tr>
<tr>
<td>
<span id="line-944" class="anchor"></span><p class="line891"><tt>wx.LIST_NEXT_LEFT</tt></p>
</td>
<td><p class="line862">查找显示上位于开始项目左边的下一个为指定状态的项目。</p></td>
</tr>
<tr>
<td>
<span id="line-945" class="anchor"></span><p class="line891"><tt>wx.LIST_NEXT_RIGHT</tt></p>
</td>
<td><p class="line862">查找显示上位于开始项目右边的下一个为指定状态的项目。</p></td>
</tr>
</tbody></table></div>
<span id="line-946" class="anchor"></span><span id="line-947" class="anchor"></span><p class="line862">表13.11列出了用于<tt>GetNextItem()</tt>的<tt>state</tt>参数的取值 <span id="line-948" class="anchor"></span><span id="line-949" class="anchor"></span><span id="line-950" class="anchor"></span></p>
<p class="line867"><strong>表13.11</strong>  <span id="line-951" class="anchor"></span><strong>用于<tt>GetNextItem()</tt>的<tt>state</tt>参数的取值</strong> <span id="line-952" class="anchor"></span><span id="line-953" class="anchor"></span></p>
<div><table><tbody>
<tr>
<td><p class="line891"><tt>wx.LIST_STATE_CUT</tt></p></td>
<td><p class="line862">只查找所选择的用于剪贴板剪切和粘贴的项目。</p></td>
</tr>
<tr>
<td>
<span id="line-954" class="anchor"></span><p class="line891"><tt>wx.LIST_STATE_DONTCARE</tt></p>
</td>
<td><p class="line862">查找项目，不管它当前的状态。</p></td>
</tr>
<tr>
<td>
<span id="line-955" class="anchor"></span><p class="line891"><tt>wx.LIST_STATE_DropHILITED</tt></p>
</td>
<td><p class="line862">只查找鼠标要释放的项目。</p></td>
</tr>
<tr>
<td>
<span id="line-956" class="anchor"></span><p class="line891"><tt>wx.LIST_STATE_FOCUSED</tt></p>
</td>
<td><p class="line862">只查找当前有焦点的项目。</p></td>
</tr>
<tr>
<td>
<span id="line-957" class="anchor"></span><p class="line891"><tt>wx.LIST_STATE_SelectED</tt></p>
</td>
<td><p class="line862">只查找当前被选择的项目。</p></td>
</tr>
</tbody></table></div>
<span id="line-958" class="anchor"></span><span id="line-959" class="anchor"></span><p class="line874">表13.12显示了用于改变一个项目的文本显示的方法以及用于控件项目的字体和颜色的方法。 <span id="line-960" class="anchor"></span><span id="line-961" class="anchor"></span><span id="line-962" class="anchor"></span></p>
<p class="line867"><strong>表13.2</strong>  <span id="line-963" class="anchor"></span><strong>列表控件的显示属性</strong> <span id="line-964" class="anchor"></span><span id="line-965" class="anchor"></span></p>
<div><table><tbody>
<tr>
<td><p class="line891"><tt>GetBackgroundColour()</tt></p></td>
<td style="text-align: center;" rowspan="2" colspan="1"><p class="line862">处理整个列表控件的背景色。参数<tt>col</tt>是一个<tt>wx.Colour</tt>或颜色名。</p></td>
</tr>
<tr>
<td>
<span id="line-966" class="anchor"></span><p class="line891"><tt>SetBackgroundColour(col)</tt></p>
</td>
</tr>
<tr>
<td>
<span id="line-967" class="anchor"></span><p class="line891"><tt>GetItemBackgroundColour(item)</tt></p>
</td>
<td style="text-align: center;" rowspan="2" colspan="1"><p class="line862">处理索引<tt>item</tt>所指定的项目的背景色。这个属性只用于报告模式。</p></td>
</tr>
<tr>
<td>
<span id="line-968" class="anchor"></span><p class="line891"><tt>SetItemBackgroundColour(item,col)</tt></p>
</td>
</tr>
<tr>
<td>
<span id="line-969" class="anchor"></span><p class="line891"><tt>GetItemTextColour(item)</tt></p>
</td>
<td style="text-align: center;" rowspan="2" colspan="1"><p class="line862">处理索引<tt>item</tt>所指定的项目的文本的颜色。这个属性只用于报告模式。</p></td>
</tr>
<tr>
<td>
<span id="line-970" class="anchor"></span><p class="line891"><tt>SetItemTextColour(item, col)</tt></p>
</td>
</tr>
<tr>
<td>
<span id="line-971" class="anchor"></span><p class="line891"><tt>GetTextColour()</tt></p>
</td>
<td style="text-align: center;" rowspan="2" colspan="1"><p class="line862">处理整个列表的文本的颜色。</p></td>
</tr>
<tr>
<td>
<span id="line-972" class="anchor"></span><p class="line891"><tt>SetTextColour(col)</tt></p>
</td>
</tr>
</tbody></table></div>
<span id="line-973" class="anchor"></span><span id="line-974" class="anchor"></span><p class="line874">表13.3显示了列表控件的其它的一些方法。 <span id="line-975" class="anchor"></span><span id="line-976" class="anchor"></span><span id="line-977" class="anchor"></span></p>
<p class="line867"><strong>表13.3</strong>  <span id="line-978" class="anchor"></span><strong>列表控件的其它的一些方法</strong> <span id="line-979" class="anchor"></span><span id="line-980" class="anchor"></span></p>
<div><table><tbody>
<tr>
<td><p class="line891"><tt>GetItemSpacing()</tt></p></td>
<td><p class="line862">返回位于图标间的空白的<tt>wx.Size</tt>。单位为像素。</p></td>
</tr>
<tr>
<td>
<span id="line-981" class="anchor"></span><p class="line891"><tt>GetSelectedItemCount()</tt></p>
</td>
<td><p class="line862">返回列表中当前被选择的项目的数量。</p></td>
</tr>
<tr>
<td>
<span id="line-982" class="anchor"></span><p class="line891"><tt>GetTopItem()</tt></p>
</td>
<td><p class="line862">返回可见区域顶部的项目的索引。只在报告模式中有意义。</p></td>
</tr>
<tr>
<td>
<span id="line-983" class="anchor"></span><p class="line891"><tt>GetViewRect()</tt></p>
</td>
<td><p class="line862">返回一个<tt>wx.Rect</tt>，它是能够包含所有项目所需的最小矩形（没有滚动条）。只对图标或小图标模式有意义。</p></td>
</tr>
<tr>
<td>
<span id="line-984" class="anchor"></span><p class="line891"><tt>ScrollList(dx, dy)</tt></p>
</td>
<td><p class="line862">使用控件滚动。参数<tt>dy</tt>是垂直量，<tt>dx</tt>是水平量，单位是像素。对于图标、小图标或报告模式，单位是像素。如果是列表模式，那么单位是列数。</p></td>
</tr>
</tbody></table></div>
<span id="line-985" class="anchor"></span><span id="line-986" class="anchor"></span><span id="line-987" class="anchor"></span><p class="line874">上面的这些表涉及了一个列表控件的大多数功能。然而到目前为止，我们所见过的所有的列表控件，它们被限制为：在程序的运行期间，它们的所有数据必须存在于内存中。在下一节，我们将讨论一个机制，这个机制仅在数据需要被显示时，才提供列表数据。 <span id="line-988" class="anchor"></span><span id="line-989" class="anchor"></span><span id="line-990" class="anchor"></span></p>
<p class="line867">
</p>
<h2 id="head-00dfa2b9c1536fcca296d7fa8616f3cdc3872d37">4. 创建一个虚列表控件</h2>
<span id="line-991" class="anchor"></span><span id="line-992" class="anchor"></span><span id="line-993" class="anchor"></span><p class="line862">让我们设想你的<tt>wxPython</tt>应用程序需要去显示包含你所有客户的一个列表。开始时你使用一个标准的列表控件，并且它工作的很好。后来人的客户列表变得越来越大，太多的客户使得你的应用程序开始出现了效率问题。这时你的程序起动所需的时间变得较长了，并占用越来越多的内存。你怎么办呢？你可以创建一个虚的列表控件。 <span id="line-994" class="anchor"></span><span id="line-995" class="anchor"></span></p>
<p class="line874">问题的实质就是列表控件的数据处理。通常，这些数据都是从数据产生的地方将数据拷贝到列表控件中。这是潜在地浪费资源，对于一个小的列表，这好象看不出任何问题，但对于创建一个较大的列表控件，这将占用很多的内存，并导致启动变慢。 <span id="line-996" class="anchor"></span><span id="line-997" class="anchor"></span></p>
<p class="line862">为了将一个列表控件所占的内存和启动所需的时间降到最小化，<tt>wxPython</tt>允许你去声明一个虚的列表控件，这意味关于每项的信息只在控件需要去显示该项时才生成。这就防止了控件一开始就将每项存储到它的内存空间中，并且这也意味着在启动时，并没有声明完整的列表控件。同时这个方案的缺点就是虚列表中的列表项的恢复可能变得较慢。图13.6显示了一个虚列表。
<span id="line-998" class="anchor"></span><span id="line-999" class="anchor"></span></p>
<p class="line874">例13.5显示了产生该虚列表控件的完整代码 <span id="line-1000" class="anchor"></span><span id="line-1001" class="anchor"></span><span id="line-1002" class="anchor"></span></p>
<p class="line867"><strong>图13.6</strong> <span id="line-1003" class="anchor"></span><span id="line-1004" class="anchor"></span></p>
<p class="line867"><img title="attachment:w13.6.gif" src="/moin/WxPythonInAction/ChapterThirteen?action=AttachFile&amp;do=get&amp;target=w13.6.gif" class="attachment"><span id="line-1005" class="anchor"></span><span id="line-1006" class="anchor"></span><span id="line-1007" class="anchor"></span><span id="line-1008" class="anchor"></span></p>
<p class="line867"><strong>例13.5</strong>  <span id="line-1009" class="anchor"></span><strong>一个虚列表控件</strong> <span id="line-1010" class="anchor"></span><span id="line-1011" class="anchor"></span><span id="line-1012" class="anchor"></span><span id="line-1013" class="anchor"></span><span id="line-1014" class="anchor"></span><span id="line-1015" class="anchor"></span><span id="line-1016" class="anchor"></span><span id="line-1017" class="anchor"></span><span id="line-1018" class="anchor"></span><span id="line-1019" class="anchor"></span><span id="line-1020" class="anchor"></span><span id="line-1021" class="anchor"></span><span id="line-1022" class="anchor"></span><span id="line-1023" class="anchor"></span><span id="line-1024" class="anchor"></span><span id="line-1025" class="anchor"></span><span id="line-1026" class="anchor"></span><span id="line-1027" class="anchor"></span><span id="line-1028" class="anchor"></span><span id="line-1029" class="anchor"></span><span id="line-1030" class="anchor"></span><span id="line-1031" class="anchor"></span><span id="line-1032" class="anchor"></span><span id="line-1033" class="anchor"></span><span id="line-1034" class="anchor"></span><span id="line-1035" class="anchor"></span><span id="line-1036" class="anchor"></span><span id="line-1037" class="anchor"></span><span id="line-1038" class="anchor"></span><span id="line-1039" class="anchor"></span><span id="line-1040" class="anchor"></span><span id="line-1041" class="anchor"></span><span id="line-1042" class="anchor"></span><span id="line-1043" class="anchor"></span><span id="line-1044" class="anchor"></span><span id="line-1045" class="anchor"></span><span id="line-1046" class="anchor"></span><span id="line-1047" class="anchor"></span><span id="line-1048" class="anchor"></span><span id="line-1049" class="anchor"></span><span id="line-1050" class="anchor"></span><span id="line-1051" class="anchor"></span><span id="line-1052" class="anchor"></span><span id="line-1053" class="anchor"></span><span id="line-1054" class="anchor"></span><span id="line-1055" class="anchor"></span><span id="line-1056" class="anchor"></span><span id="line-1057" class="anchor"></span><span id="line-1058" class="anchor"></span><span id="line-1059" class="anchor"></span><span id="line-1060" class="anchor"></span><span id="line-1061" class="anchor"></span><span id="line-1062" class="anchor"></span><span id="line-1063" class="anchor"></span><span id="line-1064" class="anchor"></span><span id="line-1065" class="anchor"></span><span id="line-1066" class="anchor"></span><span id="line-1067" class="anchor"></span><span id="line-1068" class="anchor"></span><span id="line-1069" class="anchor"></span><span id="line-1070" class="anchor"></span><span id="line-1071" class="anchor"></span><span id="line-1072" class="anchor"></span><span id="line-1073" class="anchor"></span><span id="line-1074" class="anchor"></span><span id="line-1075" class="anchor"></span><span id="line-1076" class="anchor"></span><span id="line-1077" class="anchor"></span><span id="line-1078" class="anchor"></span><span id="line-1079" class="anchor"></span></p>
<div lang="en" dir="ltr" class="codearea">
<script type="text/javascript">
document.write('<a href="#" onclick="return togglenumber(\'CA-d3d0e464ebcfeb940225eca08b3288324f8fb71e_009\', 1, 1);" \
class="codenumbers">切换行号显示<\/a>');
</script><a class="codenumbers" onclick="return togglenumber('CA-d3d0e464ebcfeb940225eca08b3288324f8fb71e_009', 1, 1);" href="#">切换行号显示</a>
<pre lang="en" id="CA-d3d0e464ebcfeb940225eca08b3288324f8fb71e_009" dir="ltr"><span class="line"><span class="LineNumber">   1 </span><span class="Comment">#!/usr/bin/python</span></span>
<span class="line"><span class="LineNumber">   2 </span><span class="Comment"></span><span class="Comment">#-*- encoding:UTF-8 -*-</span></span>
<span class="line"><span class="LineNumber">   3 </span><span class="Comment"></span><span class="ResWord">import</span> <span class="ID">wx</span></span>
<span class="line"><span class="LineNumber">   4 </span><span class="ResWord">import</span> <span class="ID">sys</span><span class="Operator">,</span> <span class="ID">glob</span><span class="Operator">,</span> <span class="ID">random</span></span>
<span class="line"><span class="LineNumber">   5 </span><span class="ResWord">import</span> <span class="ID">data</span></span>
<span class="line"><span class="LineNumber">   6 </span></span>
<span class="line"><span class="LineNumber">   7 </span><span class="ResWord">class</span> <span class="ID">DataSource</span><span class="Operator">:</span><span class="Comment">#数据源</span></span>
<span class="line"><span class="LineNumber">   8 </span>    <span class="String">"""</span></span>
<span class="line"><span class="LineNumber">   9 </span><span class="String">    A simple data source class that just uses our sample data items.</span></span>
<span class="line"><span class="LineNumber">  10 </span><span class="String">    A real data source class would manage fetching items from a</span></span>
<span class="line"><span class="LineNumber">  11 </span><span class="String">    database o similar.</span></span>
<span class="line"><span class="LineNumber">  12 </span><span class="String">    """</span></span>
<span class="line"><span class="LineNumber">  13 </span>    <span class="ResWord">def</span> <span class="ID">GetColumnHeaders</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  14 </span>        <span class="ResWord">return</span> <span class="ID">data</span><span class="Operator">.</span><span class="ID">columns</span></span>
<span class="line"><span class="LineNumber">  15 </span></span>
<span class="line"><span class="LineNumber">  16 </span>    <span class="ResWord">def</span> <span class="ID">GetCount</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  17 </span>        <span class="ResWord">return</span> <span class="ID">len</span><span class="Operator">(</span><span class="ID">data</span><span class="Operator">.</span><span class="ID">rows</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  18 </span></span>
<span class="line"><span class="LineNumber">  19 </span>    <span class="ResWord">def</span> <span class="ID">GetItem</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">index</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  20 </span>        <span class="ResWord">return</span> <span class="ID">data</span><span class="Operator">.</span><span class="ID">rows</span><span class="Operator">[</span><span class="ID">index</span><span class="Operator">]</span></span>
<span class="line"><span class="LineNumber">  21 </span></span>
<span class="line"><span class="LineNumber">  22 </span>    <span class="ResWord">def</span> <span class="ID">UpdateCache</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">start</span><span class="Operator">,</span> <span class="ID">end</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  23 </span>        <span class="ResWord">pass</span></span>
<span class="line"><span class="LineNumber">  24 </span></span>
<span class="line"><span class="LineNumber">  25 </span></span>
<span class="line"><span class="LineNumber">  26 </span><span class="ResWord">class</span> <span class="ID">VirtualListCtrl</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">ListCtrl</span><span class="Operator">)</span><span class="Operator">:</span><span class="Comment">#1 声明虚列表</span></span>
<span class="line"><span class="LineNumber">  27 </span>    <span class="String">"""</span></span>
<span class="line"><span class="LineNumber">  28 </span><span class="String">    A generic virtual listctrl that fetches data from a DataSource.</span></span>
<span class="line"><span class="LineNumber">  29 </span><span class="String">    """</span></span>
<span class="line"><span class="LineNumber">  30 </span>    <span class="ResWord">def</span> <span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">parent</span><span class="Operator">,</span> <span class="ID">dataSource</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  31 </span>        <span class="ID">wx</span><span class="Operator">.</span><span class="ID">ListCtrl</span><span class="Operator">.</span><span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">parent</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">  32 </span>            <span class="ID">style</span><span class="Operator">=</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">LC_REPORT</span><span class="Operator">|</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">LC_SINGLE_SEL</span><span class="Operator">|</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">LC_VIRTUAL</span><span class="Operator">)</span><span class="Comment">#使用wx.LC_VIRTUAL标记创建虚列表</span></span>
<span class="line"><span class="LineNumber">  33 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">dataSource</span> <span class="Operator">=</span> <span class="ID">dataSource</span></span>
<span class="line"><span class="LineNumber">  34 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">Bind</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">EVT_LIST_CACHE_HINT</span><span class="Operator">,</span>
<span class="ID">self</span><span class="Operator">.</span><span class="ID">DoCacheItems</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  35 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">SetItemCount</span><span class="Operator">(</span><span class="ID">dataSource</span><span class="Operator">.</span><span class="ID">GetCount</span><span class="Operator">(</span><span class="Operator">)</span><span class="Operator">)</span><span class="Comment">#设置列表的大小</span></span>
<span class="line"><span class="LineNumber">  36 </span></span>
<span class="line"><span class="LineNumber">  37 </span>        <span class="ID">columns</span> <span class="Operator">=</span> <span class="ID">dataSource</span><span class="Operator">.</span><span class="ID">GetColumnHeaders</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  38 </span>        <span class="ResWord">for</span> <span class="ID">col</span><span class="Operator">,</span> <span class="ID">text</span> <span class="ResWord">in</span> <span class="ID">enumerate</span><span class="Operator">(</span><span class="ID">columns</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  39 </span>            <span class="ID">self</span><span class="Operator">.</span><span class="ID">InsertColumn</span><span class="Operator">(</span><span class="ID">col</span><span class="Operator">,</span> <span class="ID">text</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  40 </span></span>
<span class="line"><span class="LineNumber">  41 </span></span>
<span class="line"><span class="LineNumber">  42 </span>    <span class="ResWord">def</span> <span class="ID">DoCacheItems</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">evt</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  43 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">dataSource</span><span class="Operator">.</span><span class="ID">UpdateCache</span><span class="Operator">(</span></span>
<span class="line"><span class="LineNumber">  44 </span>            <span class="ID">evt</span><span class="Operator">.</span><span class="ID">GetCacheFrom</span><span class="Operator">(</span><span class="Operator">)</span><span class="Operator">,</span> <span class="ID">evt</span><span class="Operator">.</span><span class="ID">GetCacheTo</span><span class="Operator">(</span><span class="Operator">)</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  45 </span></span>
<span class="line"><span class="LineNumber">  46 </span>    <span class="ResWord">def</span> <span class="ID">OnGetItemText</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">item</span><span class="Operator">,</span> <span class="ID">col</span><span class="Operator">)</span><span class="Operator">:</span><span class="Comment">#得到需求时的文本</span></span>
<span class="line"><span class="LineNumber">  47 </span>        <span class="ID">data</span> <span class="Operator">=</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">dataSource</span><span class="Operator">.</span><span class="ID">GetItem</span><span class="Operator">(</span><span class="ID">item</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  48 </span>        <span class="ResWord">return</span> <span class="ID">data</span><span class="Operator">[</span><span class="ID">col</span><span class="Operator">]</span></span>
<span class="line"><span class="LineNumber">  49 </span></span>
<span class="line"><span class="LineNumber">  50 </span>    <span class="ResWord">def</span> <span class="ID">OnGetItemAttr</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">item</span><span class="Operator">)</span><span class="Operator">:</span>
<span class="ResWord">return</span> <span class="ID">None</span></span>
<span class="line"><span class="LineNumber">  51 </span>    <span class="ResWord">def</span> <span class="ID">OnGetItemImage</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">item</span><span class="Operator">)</span><span class="Operator">:</span>
<span class="ResWord">return</span> <span class="Operator">-</span><span class="Number">1</span></span>
<span class="line"><span class="LineNumber">  52 </span></span>
<span class="line"><span class="LineNumber">  53 </span></span>
<span class="line"><span class="LineNumber">  54 </span></span>
<span class="line"><span class="LineNumber">  55 </span><span class="ResWord">class</span> <span class="ID">DemoFrame</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">Frame</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  56 </span>    <span class="ResWord">def</span> <span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  57 </span>        <span class="ID">wx</span><span class="Operator">.</span><span class="ID">Frame</span><span class="Operator">.</span><span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">None</span><span class="Operator">,</span> <span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">  58 </span>                          <span class="String">"Virtual wx.ListCtrl"</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">  59 </span>                          <span class="ID">size</span><span class="Operator">=</span><span class="Operator">(</span><span class="Number">600</span><span class="Operator">,</span><span class="Number">400</span><span class="Operator">)</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  60 </span></span>
<span class="line"><span class="LineNumber">  61 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">list</span> <span class="Operator">=</span> <span class="ID">VirtualListCtrl</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span>
<span class="ID">DataSource</span><span class="Operator">(</span><span class="Operator">)</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  62 </span></span>
<span class="line"><span class="LineNumber">  63 </span></span>
<span class="line"><span class="LineNumber">  64 </span></span>
<span class="line"><span class="LineNumber">  65 </span><span class="ID">app</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">PySimpleApp</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  66 </span><span class="ID">frame</span> <span class="Operator">=</span> <span class="ID">DemoFrame</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  67 </span><span class="ID">frame</span><span class="Operator">.</span><span class="ID">Show</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  68 </span><span class="ID">app</span><span class="Operator">.</span><span class="ID">MainLoop</span><span class="Operator">(</span><span class="Operator">)</span><span class="Text"></span></span>
</pre>
</div>
<span id="line-1080" class="anchor"></span><span id="line-1081" class="anchor"></span><p class="line874">这个数据源的类是一个简单的例子，它存储了我们所需要的数据项。真实情况下的数据源类还会处理从一个数据库中获取数据之类的情况，这种情况下只需要重新实现本例中的同一接口。 <span id="line-1082" class="anchor"></span><span id="line-1083" class="anchor"></span></p>
<p class="line862">要创建一个虚列表，第一步就是在初始化的时候对列表控件使用<tt>wx.LC_VIRTUAL</tt>标记如#1。通常使用子类<tt>wx.ListCtrl</tt>来创建你的虚列表控件，而非仅仅使用构造函数。这是因为你需要覆盖<tt>wx.ListCtrl</tt>的一些方法，以便扩展这个虚列表。虚列表的声明类似如下：
<span id="line-1084" class="anchor"></span><span id="line-1085" class="anchor"></span></p>
<p class="line867"><span id="line-1086" class="anchor"></span><span id="line-1087" class="anchor"></span><span id="line-1088" class="anchor"></span><span id="line-1089" class="anchor"></span><span id="line-1090" class="anchor"></span></p>
<div lang="en" dir="ltr" class="codearea">
<script type="text/javascript">
document.write('<a href="#" onclick="return togglenumber(\'CA-ab3a137b374042d4d5a1d6e751026e777dd47bbc_010\', 1, 1);" \
class="codenumbers">切换行号显示<\/a>');
</script><a class="codenumbers" onclick="return togglenumber('CA-ab3a137b374042d4d5a1d6e751026e777dd47bbc_010', 1, 1);" href="#">切换行号显示</a>
<pre lang="en" id="CA-ab3a137b374042d4d5a1d6e751026e777dd47bbc_010" dir="ltr"><span class="line"><span class="LineNumber">   1 </span><span class="ResWord">class</span> <span class="ID">MyVirtualList</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">ListCtrl</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">   2 </span>    <span class="ResWord">def</span> <span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">parent</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">   3 </span>        <span class="ID">wx</span><span class="Operator">.</span><span class="ID">ListCtrl</span><span class="Operator">.</span><span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">parent</span><span class="Operator">,</span> <span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">   4 </span>            <span class="ID">style</span><span class="Operator">=</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">LC_REPORT</span><span class="Operator">|</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">LC_VIRTUAL</span><span class="Operator">)</span><span class="Text"></span></span>
</pre>
</div>
<span id="line-1091" class="anchor"></span><span id="line-1092" class="anchor"></span><p class="line862">有时在虚列表的初始化期间，必须调用<tt>SetItemCount()</tt>方法。这将告诉控件在数据源中存在多少数据项，这样它就可以设置适当的限制并处理滚动条。如果数据源中的数据项的数量改变了，你可以再调用<tt>SetItemCount()</tt>一次。你所覆盖的任何以<tt>On</tt>开关的方法，必须能够处理[0,<tt>SetItemCount()</tt>－1]间的数据。
<span id="line-1093" class="anchor"></span><span id="line-1094" class="anchor"></span></p>
<p class="line862">你的虚列表控件可以覆盖其父类的三个方法，以便决定在列表控件中显示些什么。最重要的要覆盖的方法是<tt>OnGetItemText(item,
col)</tt>。其中的参数<tt>item</tt>和<tt>col</tt>是要绘制的单元格的行和列，方法的返回值是显示在该单元格中的文本字符串。例如，下面的方法将只显示相关单元格的坐标。 <span id="line-1095" class="anchor"></span><span id="line-1096" class="anchor"></span></p>
<p class="line867"><span id="line-1097" class="anchor"></span><span id="line-1098" class="anchor"></span><span id="line-1099" class="anchor"></span></p>
<div lang="en" dir="ltr" class="codearea">
<script type="text/javascript">
document.write('<a href="#" onclick="return togglenumber(\'CA-1006ad25a51a6de651e0f564e7a405f11211a9f3_011\', 1, 1);" \
class="codenumbers">切换行号显示<\/a>');
</script><a class="codenumbers" onclick="return togglenumber('CA-1006ad25a51a6de651e0f564e7a405f11211a9f3_011', 1, 1);" href="#">切换行号显示</a>
<pre lang="en" id="CA-1006ad25a51a6de651e0f564e7a405f11211a9f3_011" dir="ltr"><span class="line"><span class="LineNumber">   1 </span><span class="ResWord">def</span> <span class="ID">OnGetItemText</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">item</span><span class="Operator">,</span> <span class="ID">col</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">   2 </span>    <span class="ResWord">return</span> <span class="String">"Item %d, column %d"</span> <span class="Operator">%</span> <span class="Operator">(</span><span class="ID">item</span><span class="Operator">,</span> <span class="ID">col</span><span class="Operator">)</span><span class="Text"></span></span>
</pre>
</div>
<span id="line-1100" class="anchor"></span><span id="line-1101" class="anchor"></span><p class="line862">如果你想在一行中显示一个图像，你需要覆盖<tt>OnGetItemImage(item)</tt>。它的返回值是较早声明的列表控件的图像列中的一个整数索引。如果你没有覆盖这个方法，那么基类版本的<tt>OnGetItemImage</tt>将返回－1，这表明不显示图像。如果你想改变行的一些显示属性，那么你可以覆盖<tt>OnGetItemAttr(item)</tt>方法，<tt>item</tt>是行的索引，该方法返回类<tt>wx.ListItemAttr</tt>的一个实例。该类有一些<tt>get</tt>*和<tt>set</tt>*方法可以用来设置行的颜色、对齐方式等等显示属性。
<span id="line-1102" class="anchor"></span><span id="line-1103" class="anchor"></span></p>
<p class="line862">如果你的虚列表所基于的数据改变了，而你想更新显示，那么你可以使用该列表控件的<tt>RefreshItem(item)</tt>来重绘特定的行。相关的方法<tt>RefreshItems(itemFrom,itemTo)</tt>重绘位于索引<tt>itemFrom</tt>和<tt>itemTo</tt>间的所有行。 <span id="line-1104" class="anchor"></span><span id="line-1105" class="anchor"></span></p>
<p class="line862">为了对数据源中的数据的获取提供优化帮助，对于要显示一页新的数据，虚列表控件会发送<tt>EVT_LIST_CACHE_HINT</tt>事件。这将给你的数据源一个时机用以从数据库（或另处）一次获取几个记录并保存它们。这样就使得随后的<tt>OnGetItemText()</tt>执行的更快。 <span id="line-1106" class="anchor"></span><span id="line-1107" class="anchor"></span><span id="line-1108" class="anchor"></span></p>
<p class="line867">
</p>
<h2 id="head-3b786c55dd2c278300b510be26a926707dfe4b1b">5. 本章小结</h2>
<span id="line-1109" class="anchor"></span><span id="line-1110" class="anchor"></span><span id="line-1111" class="anchor"></span><p class="line862">1、列表控件是<tt>wxPython</tt>用于显示列表信息的窗口部件。它比简单的列表框部件要更复杂且有完整的特性。列表控件是类<tt>wx.ListCtrl</tt>的实例。列表控件可以显示为图标模式，每个图标下都有一个项目文本，也可以显示为带有小图标的小图标模式等等。在列表模式中，元素按列显示，在报告模式中，以多列的格式显示列表，每列都有列标签。
<span id="line-1112" class="anchor"></span><span id="line-1113" class="anchor"></span></p>
<p class="line874">2、用于列表控件的图像是由一个图像列表管理的，图像列表是一个可经由索引来访问的一个图像的数组。列表控件可以为不同的列表模式维护各自的图像列表，这使得能够容易地在模式间切换。 <span id="line-1114" class="anchor"></span><span id="line-1115" class="anchor"></span></p>
<p class="line862">3、你可以使用<tt>InsertStringItem(index,label)</tt>方法来插入文本到列表中，使用<tt>InsertImageItem(index,
imageIndex)</tt>方法插入图像到列表中。要一次做上面两件事，可以使用<tt>InsertImageStringItem(index,label,</tt> <span id="line-1116" class="anchor"></span></p>
<ul><li style="list-style-type: none;"><p class="line891"><tt>imageIndex)</tt>。要对报告模式的列表添加列，可以使用<tt>InsertColumn(col,  heading, format=</tt>"<tt>wx.LIST_FORMAT_LEFT,  width=</tt>-1)方法。一旦已经添加了列后，你就可以使用<tt>SetStringItem(index, col, label, imageId=</tt>-1)方法为新的列增加文本。 <span id="line-1117" class="anchor"></span><span id="line-1118" class="anchor"></span></p></li></ul>
<p class="line862">4、列表控件产生的几个事件可以被绑到程序的动作。这些事件项属于类<tt>wx.ListEvent</tt>。通常的事件类型包括<tt>EVT_LIST_Insert_ITEM, EVT_LIST_ITEM_ACTIVATED,</tt>和<tt>EVT_LIST_
ITEM_SelectED</tt>。 <span id="line-1119" class="anchor"></span><span id="line-1120" class="anchor"></span></p>
<p class="line862">5、如果列表控件声明时使用了<tt>wx.LC_EDIT_LABELS</tt>标记，那么用户就可以编辑列表项的文本。编辑的确认是通过按下回车键或在列表中敲击完成的，也可以通过按下<tt>Esc</tt>键来取消编辑。 <span id="line-1121" class="anchor"></span><span id="line-1122" class="anchor"></span></p>
<p class="line862">6、你可以通过在声明列表时使用<tt>wx.LC_SORT_ASCENDING</tt>或<tt>wx.LC_SORT_DESCENDING</tt>来排序列表。这将按照项目的字符串的顺序来排序列表。在报告模式中，这将根据0列的字符串来排序。你也可以使用<tt>SortItems(func)</tt>方法来创建你自定义的排序方法。对于报告模式的列表，<tt>mixin</tt>类<tt>wx.lib.mixins.listctrl.ColumnSorterMixin</tt>给了你根据用户所选择的列来排序的能力。
<span id="line-1123" class="anchor"></span><span id="line-1124" class="anchor"></span></p>
<p class="line862">7、使用了标记<tt>wx.LC_VIRTUAL</tt>声明的列表控件是一个虚列表控件。这意味着它的数据是当列表中的项目被显示时动态地确定的。对于虚列表控件，你必须覆盖<tt>OnGetItemText(item,
col)</tt>方法以返回适当的文本给所显示的行和列。你也可以使用<tt>OnGetItemImage(item)</tt>和<tt>OnGetItemAttr(item)</tt>方法来返回关于每行的图像或列表的显示属性。如果数据源的数据改变了，你可以使用<tt>RefreshItem(item)</tt>方法来更新列表的某个行或使用<tt>RefreshItems(itemFrom,
itemTo)</tt>方法来更新多个行。 <span id="line-1125" class="anchor"></span><span id="line-1126" class="anchor"></span></p>
<p class="line874">最终，你的数据将变得复杂得不能放在一个简单的列表中。你将会需要类似二维的电子表格样式的东西，这就是网格控件，我们将在下一章进行讨论。 <span id="line-1127" class="anchor"></span><span id="bottom" class="anchor"></span></p>
</div>
</div>
</td>
                    </tr></tbody></table>
</div>
        
        
        
        <td id="portal-column-two">
            <div class="visualPadding">
                <!-- The sticky notes -->
                 
            </div>
        </td>
        
        
        
        <div class="visualClear" id="clear-space-before-footer">
            <!-- -->
        </div>
        <hr class="netscape4">
<div id="portal-footer">
            <p> 中国Zope/Plone用户组 - 站长  ： <a target="_self" href="http://czug.org/blog/panjy"> 潘俊勇 </a>、
             <a target="_self" href="http://czug.org/blog/zhangbingkai"> 张炳凯 </a>
            </p>
        </div>
        <div id="portal-colophon">

            <p>
            本站由<a target="_blank" href="http://zopen.cn" alt="润普公司 内容管理 文档管理 知识管理 项目管理 Zope Plone">
                   润普公司</a>资助，
            采用 <a href="http://opensource.everydo.com/cms/">易度CMS </a> 构建。</p>

            <p class="discreet">
            广而告之：润普公司 <a href="http://everydo.com">易度云办公平台</a>，包括<a href="http://edodocs.com"> 易度文档管理系统 </a>，<a href="http://everydo.com/themes/czug.org/pm.everydo.com%0A">易度项目管理系统 </a> , <a href="http://everydo.com/tour/dept.rst"> 易度部门管理 </a> ，均采用Zope 3/BFG技术开发。
            <br><a target="_blank" href="http://www.miibeian.gov.cn/">沪ICP备05008050</a>
            </p>
            </div>
        
        
        <script type="text/javascript">
        var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
        document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
        </script><script type="text/javascript">
        try {
        var pageTracker = _gat._getTracker("UA-13170837-4");
        pageTracker._trackPageview();
        } catch(err) {}</script>
</body>
</html>
